C++11 reverse range-based for-loop

C++Server Side ProgrammingProgramming

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
raja
Published on 02-May-2019 11:53:12
Advertisements