Reversed Range-based for loop in C++ with Examples Last Updated : 08 Jul, 2020 Comments Improve Suggest changes Like Article Like Report Range-based for loops is an upgraded version of for loops. It is quite similar to for loops which is use in Python. Range-based for loop in C++ is added since C++ 11. We can reverse the process of iterating the loop by using boost::adaptors::reverse() function which is included in boost library Header. Header File: #include <boost/range/adaptor/reversed.hpp> Syntax: for (auto i : boost::adaptors::reverse(x)) Parameters: range_declaration: a declaration used to iterate over the elements in a container. Often uses the auto specifier for automatic type deduction. range_expression: An expression that represents a suitable sequence or a braced-init-list. loop_statement: An statement, typically a compound statement, which is the body of the loop. Below is the program to illustrate reverse range based loop in C++: CPP14 // C++ program for reverse // range-based for loop #include <bits/stdc++.h> // For reversing range based loop #include <boost/range/adaptor/reversed.hpp> using namespace std; // Driver Code int main() { string s = "geeksforgeeks"; int y[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; vector<int> v1{ 1, 2, 3, 4, 5, 6, 7, 8 }; // Reverse range-based for loop // to reverse string for (auto x : boost::adaptors::reverse(s)) cout << x << " "; cout << endl; // Reverse range-based for loop // to reverse array for (auto x : boost::adaptors::reverse(y)) cout << x << " "; cout << endl; // Reverse range-based for loop // to reverse vector for (auto x : boost::adaptors::reverse(v1)) cout << x << " "; cout << endl; return 0; } Output: s k e e g r o f s k e e g 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 Comment More infoAdvertise with us Next Article Reversed Range-based for loop in C++ with Examples kothariji Follow Improve Article Tags : Algorithms Analysis of Algorithms Searching Advanced Data Structure Competitive Programming C++ Programs C++ C++ Quiz DSA loop +6 More Practice Tags : CPPAdvanced Data StructureAlgorithmsSearching Similar Reads Range-Based for Loop with a Set in C++ In C++, a set is a container that stores unique elements in a particular order. A range-based for loop is a feature added in C++11 to iterate over containers. In this article, we will learn how to use a range-based for loop with a set in C++. Example:Input:mySet = {1, 2, 3, 4, 6}Output:1 2 3 4 6Rang 2 min read Range-Based For Loop with a Map in C++ In C++, a map is a container that stores elements formed by a combination of a key value and a mapped value. A range-based for loop is a feature added in C++11 to iterate over containers. In this article, we will learn how to use a range-based for loop with a map in C++. Example:Input:myMap: { {1, â 2 min read How to Use a Range-Based for Loop with a Vector in C++? C++ introduced the range-based for loop as a convenient way to iterate over elements in a container. In this article, we'll look at how to iterate over a vector using a range-based loop in C++. Example Input:myVector: {1, 2, 3, 4, 5}Output:1 2 3 4 5Range-Based for Loop with Vector in C++The syntax o 1 min read How to Traverse a Vector using for_each Loop in C++? In C++, vectors are dynamic containers that can change their size automatically during the insertion and deletion of elements. In this article, we will learn how we can use a for_each loop to traverse elements of a vector in C++. Example Input: myVector = {1,2,3,4,5} Output: // Squared each element 2 min read How to Traverse Set using for_each Loop in C++? In C++, STL provides a for_each algorithm which works as a loop for the given range and implements the given function for each element in the range. In this article, we will learn how to use a for_each loop with a set in C++ STL. Traverse Set using for_each Loop in C++To traverse a set using a for_e 2 min read Const vs Regular iterators in C++ with examples Prerequisite: Iterators in STL Iterators are objects similar to pointers which are used to iterate over a sequence and manipulate the container elements. The advantage of using an iterator is that it reduces the lines of code to a single statement as they allow us to manipulate the built-in arrays i 3 min read How to Reverse a Deque in C++? In C++ STL, we have a container called deque(short for double-ended queue) that allows fast insertion and deletion operations at both the beginning and end. In this article, we will learn how to reverse a deque in C++. Example: Input: myDeque = {1, 2, 3, 4, 5}; Output: Reversed Deque: 5 4 3 2 1Rever 2 min read How to Traverse a List with const_iterator in C++? In C++, a list is a container used to store data in non-contiguous memory locations. It also provides a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a list with const_iterator in C++. Example Input: myList = {10,20,30,40,50} 2 min read How to Traverse a Set with const_iterator in C++? In C++, sets are a type of associative container in which each element has to be unique because the value of the element identifies it. It contains a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a set with const_iterator in 2 min read How to Reverse a String in Place in C++? In C++, reversing a string is a basic operation in programming that is required in various applications, from simple and complex algorithms. Reversing a string in place involves changing the characters of the string directly without using input-dependent additional storage. In this article, we learn 2 min read Like