Reputation: 14827
Since the nonmember begin()
and end()
functions were added for standard contains in the C++11 revision, why have the nonmember versions of the rbegin()
and rend()
functions not been added as well? I feel silly after beginning to use the nonmember versions of begin()
and end()
, only to find that I now have to toggle between the using the member and nonmember function calls. (I realize that it would be trivial to roll my own nonmember versions of rbegin()
and rend()
, but I'm wondering why this wasn't added to the standard).
Thanks for your input.
Upvotes: 17
Views: 2348
Reputation: 1063
For people who see this later, nonmember rbegin()
and rend()
are already in C++14.
Upvotes: 7
Reputation: 137890
You can construct reversed range by manually using std::reverse_iterator
on the results of std::begin
and std::end
.
Oddly, there is not a standard factory function for reverse_iterator
. If there were, it would probably look like this:
template< typename iter >
std::reverse_iterator< iter > reverse( iter i )
{ return { i }; }
Armed with this, you can do
std::sort( reverse( std::end( my_array ) ), reverse( std::begin( my_array ) ) );
This example saves the trouble of specifying the std::greater
comparator, but the reverse_iterator
conceivably could adversely affect performance if the compiler can't remove the added complexity from inner loops.
Upvotes: 5