Why do we have to put std when sorting an array via begin and end iterators when that's not the case with vector,list etc?
example:
std::sort( std::begin(array), std::end(array), [](int a,int b) { return a>b;} );
Why do we have to put std when sorting an array via begin and end iterators when that's not the case with vector,list etc?
example:
std::sort( std::begin(array), std::end(array), [](int a,int b) { return a>b;} );
This is due to argument dependant lookup. That vector
's iterators allow you to omit std::
is not guaranteed by the standard, but it is a fairly common implementation detail.
If the result type of std::vector::begin()
is a class type defined in std
, then sort(vec.begin(), vec.end())
will look in std
for sort
as well as the global namespace, and finds std::sort
(and nothing in the global namespace).
The result of std::begin(array)
is a pointer, which is not a type from std
, so sort(std::begin(array), std::end(array))
only looks in the global namespace, and doesn't find any functions there.