One of the addition to the C++ 2011 standard that is perhaps not so much popularized is the non-member begin() and end(). In STL all containers have a non-static member begin() and end() methods that return an iterator to the beginning and the end of the container. Therefor iterating over a container could look like this:

std::vector<int> v; for(auto it = v.begin(); it != v.end(); ++it) std::cout << *it << std::endl; 1 2 3 std :: vector < int > v ; for ( auto it = v . begin ( ) ; it != v . end ( ) ; ++ it ) std :: cout << * it << std :: endl ;

The problem here is that not all user-defined containers have begin() and end(), which makes them impossible to use with the STL algorithms or any other user-defined template function that requires iterators. That is even more problematic when using C arrays. For instance, using a C array with a standard algorithm is quite different than using a vector, for instance.

int inc(n) {return n+1;} int a[] = {1,2,3,4,5}; std::transform(&a[0], &a[0] + sizeof(a)/sizeof(a[0]), &a[0], inc); std::vector<int> v(&a[0], &a[0] + sizeof(a)/sizeof(a[0])); std::transform(v.begin(), v.end(), v.begin(), inc); 1 2 3 4 5 6 7 int inc ( n ) { return n + 1 ; } int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; std :: transform ( &a [ 0 ] , &a [ 0 ] + sizeof ( a ) / sizeof ( a [ 0 ] ) , &a [ 0 ] , inc ) ; std :: vector < int > v ( &a [ 0 ] , &a [ 0 ] + sizeof ( a ) / sizeof ( a [ 0 ] ) ) ; std :: transform ( v . begin ( ) , v . end ( ) , v . begin ( ) , inc ) ;

The non-member begin() and end() methods are extensible, in the sense they can be overloaded for any type (including C arrays). Herb Sutter argues in his Elements of Modern C++ Style article that you should always prefer the non-member version. They promote uniformity and consistency and allow for more generic programming.

Always use nonmember begin(x) and end(x) (not x.begin() and x.end()), because begin(x) and end(x) are extensible and can be adapted to work with all container types – even arrays – not just containers that follow the STL style of providing x.begin() and x.end() member functions.

The code above can now look like this:

std::vector<int> v {1,2,3,4,5}; for(auto it = begin(v); it != end(v); ++it) std::cout << *it << std::endl; std::transform(begin(v), end(v), begin(v), [](int n){return n+1;}); 1 2 3 4 5 std :: vector < int > v { 1 , 2 , 3 , 4 , 5 } ; for ( auto it = begin ( v ) ; it != end ( v ) ; ++ it ) std :: cout << * it << std :: endl ; std :: transform ( begin ( v ) , end ( v ) , begin ( v ) , [ ] ( int n ) { return n + 1 ; } ) ;

As for the C array we can overload begin() and end() to look maybe like this:

template <typename T, size_t size> T* begin(T (&c)[size]) { return &c[0]; } template <typename T, size_t size> T* end(T (&c)[size]) { return &c[0] + size; } 1 2 3 4 5 6 7 8 9 10 11 template < typename T , size_t size > T * begin ( T ( &c ) [ size ] ) { return &c [ 0 ] ; } template < typename T , size_t size > T * end ( T ( &c ) [ size ] ) { return &c [ 0 ] + size ; }

With this available we can write:

int a[] = {1,2,3,4,5}; std::transform(begin(a), end(a), begin(a), [](int n) {return n+1;}); for(auto it = begin(a); it != end(a); ++it) std::cout << *it << std::endl; 1 2 3 4 5 6 int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; std :: transform ( begin ( a ) , end ( a ) , begin ( a ) , [ ] ( int n ) { return n + 1 ; } ) ; for ( auto it = begin ( a ) ; it != end ( a ) ; ++ it ) std :: cout << * it << std :: endl ;

If you’d argue that non-member begin() and end() break the encapsulation then I suggest you read Scott Meyers’ How Non-Member Functions Improve Encapsulation where he explains the opposite.

If you’re writing a function that can be implemented as either a member or as a non-friend non-member, you should prefer to implement it as a non-member function. That decision increases class encapsulation. When you think encapsulation, you should think non-member functions.

There is still one question left: what about the other container functions that return iterators, rbegin(), rend(), cbegin(), cend(), crbegin(), and crend()? Currently they are not implemented as non-member functions. According to Herb Sutter, the omission of cbegin() and cend() was an oversight and it will be fixed.

Additional readings:

CodeProject

Share this: Twitter

LinkedIn

StumbleUpon

Facebook

Reddit

More

Google

Email



Print

