Click here to Skip to main content
13,145,774 members (68,190 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 5 Oct 2012

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

, 7 Oct 2012
Rate this:
Please Sign up or sign in to vote.
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:

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);

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;});

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;

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;

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


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


About the Author

Marius Bancila
Architect Visma Software
Romania Romania
Marius Bancila is the author of Modern C++ Programming Cookbook. He was a Microsoft MVP for VC++ and later Visual Studio and Development Technologies for 11 years. He works as a system architect for Visma, a Norwegian-based company. He is mainly focused on building desktop applications with VC++ and VC#. He keeps a blog at, focused on Windows programming. He is the co-founder of, a community for Romanian C++ programmers.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
osy7-Oct-12 21:09
memberosy7-Oct-12 21:09 
GeneralRe: My vote of 5 Pin
Marius Bancila7-Oct-12 21:45
memberMarius 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 8 Oct 2012
Article Copyright 2012 by Marius Bancila
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid