Difference between std :: end (myVector) and myVector.end ()

I noticed that there are two ways to get the final iterator of a vector (or another container class):

std::end(myVector) 

and

 myVector.end() 

The same applies to the other functions of the container iterator, begin , cend , cbegin , rend , rbegin , crend , crbegin , find , etc. I wonder if there is any functional difference between the two? And if not, is there any historical reason to have both of them?

(Sorry, if this is a duplicate, I searched everything and found many sources for one or the other of these methods, but neither of them mentions both or compares them.)

+6
source share
1 answer

There is a historical reason: before C ++ 11, there were only versions of member functions. Non-members have been added in C ++ 11, which also work for simple C-style arrays, so they can be considered more general.

 int a[] = {3, 1, 5, 67, 28, -12}; std::sort(std::begin(a), std::end(a)); 

When applying the standard library to containers, the effect of std::begin and std::end consists in calling the member functions of the begin() and end() container, so there is no functional difference.

C ++ 14 added std::cbegin , std::cend , std::rbegin , std::rend , std::crbegin and std::crend , with similar behavior.

+7
source

All Articles