STL sort algorithm

The general syntax of the STL sort algorithm is

sort(myvector.begin(),myvector.end()); //used for sorting an array of integers in the ascending order

The code for sorting an array of integers in the descending order is however

sort(myvector.rbegin(),myvector.rend());

I was wondering if we could just use

sort(myvector.end(),myvector.begin());

as they are already defined. Is it legal to do so or will it generate some kind of error? Why create new methods when we already have a method existing to do the exact same thing?

The STL sort() function uses Random Access Iterators that sort the vector from [first,last), where first is the starting position(inclusive) and last is the ending position(exclusive).

sort(myvector.end(),myvector.begin()) will give a segmentation fault as the function will try to access un-mapped memory.

1 Like

Alright, thanks!