Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It there an inverse range-based for in C++11? [duplicate]

Tags:

c++

c++11

stl

Possible Duplicate:
C++11 reverse range-based for-loop

Is there an inverse range-based for in C++11?

I want to do something like this:

for(int value : vec)
{
    cout << value << endl;
}

To do this:

for(auto it = vec.rbegin(); it != vec.rend(); ++it)
{
    cout << *it << endl;
}

For instance:

for(int value : -vec)
{
    cout << value << endl;
}    

Is possible do something like that to do an inverse loop?

like image 862
Lucas Lima Avatar asked Sep 10 '12 22:09

Lucas Lima


1 Answers

You can just use Boost.Range's reversed adaptor:

for(int value : ( vec | boost::adaptors::reversed ))
{...}

But standard C++11 doesn't have a similar feature.

like image 78
Nicol Bolas Avatar answered Sep 23 '22 18:09

Nicol Bolas