C++11 から begin() と end() と言う面白い関数が出来た2020年04月20日 12時07分32秒

C++11 で、iterator ヘッダファイルに begin と end と言う関数が導入された。メンバ関数として、コンテナにある begin と end と同じ様な動作をする。

基本的にはコンテナ型の様に使うのだが、配列の様に純粋なコンテナ型では無いものにも使える。

#include <iterator>
#include <iostream>
#include <algorithm>

int main()
{
    int numbers[] = { 1, 2, 3, 4, -2,  5, 0, -1 };

    std::for_each( numbers, std::end( numbers ),
        []( int i ) { std::cout << i << std::endl; } );
}
整数型の配列を cout で出力できる。
% c++ -std=c++11 end.cpp
% ./a.out 
1
2
3
4
-2
5
0
-1