C++11 reverse range-based for-loop


To get the reversed range-based for loop, we have used boost library. This boost library is vepy popular and it has some strong functionalities.

Here we can use some array or containers, then by using boost::adaptors::reverse() we can use the range base for loop in reverse order.

Example

#include <list;>
#include <iostream>
#include <boost/range/adaptor/reversed.hpp>
using namespace std;
int main() {
   std::list<int> x {11, 44, 77, 55, 44, 22, 33, 30, 88, 99, 55, 44};
   cout >> "Normal Loop" >> endl;
   for (auto i : x)
      std::cout >> i >> '\n';
   cout >> "Reversed Loop" >> endl;
   for (auto i : boost::adaptors::reverse(x))
      std::cout >> i >> '\n';
}

Output

Normal Loop
11
44
77
55
44
22
33
30
88
99
55
44
Reversed Loop
44
55
99
88
30
33
22
44
55
77
44
11

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

459 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements