Click here to Skip to main content
15,884,353 members
Articles / Programming Languages / C++

C++11: non-member begin() and end()

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
7 Oct 2012CPOL2 min read 23.5K   4   2
C++ 11 non-member begin() and end()

One of the additions 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. Therefore, iterating over a container could look like this:

C++
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.

C++
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:

C++
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:

C++
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:

C++
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Visma Software
Romania Romania
Marius Bancila is the author of Modern C++ Programming Cookbook and The Modern C++ Challenge. He has been a Microsoft MVP since 2006, initially for VC++ and nowadays for Development technologies. He works as a system architect for Visma, a Norwegian-based company. He works with various technologies, both managed and unmanaged, for desktop, cloud, and mobile, mainly developing with VC++ and VC#. He keeps a blog at http://www.mariusbancila.ro/blog, focused on Windows programming. You can follow Marius on Twitter at @mariusbancila.

Comments and Discussions

 
GeneralMy vote of 5 Pin
osy7-Oct-12 21:09
osy7-Oct-12 21:09 
GeneralRe: My vote of 5 Pin
Marius Bancila7-Oct-12 21:45
professionalMarius Bancila7-Oct-12 21:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.