How to Traverse a Set with reverse_iterator in C++? Last Updated : 21 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 provides reverse iterators that make it easier to traverse the set in reverse direction. In this article, we will discuss how to traverse a set with reverse_iterator in C++. Example Input:input_set = {70,10,50,90,60,40,100,30,20,80Output:100, 90, 80, 70, 60, 50, 40, 30, 20, 10Traverse a Set with reverse_iterator in C++Reverse iterators in C++ are used to traverse the set backward. The std::set has two member functions that provide the reverse iterators: set::begin(): Return the reverse iterator pointing to the last element in the set.set::rend(): Return a reverse iterator pointing to the theoretical element right before the first element in the set.C++ Program to Traverse a Set with reverse_iterator C++ // C++ program to traverse the set using the // reverse_iterator. #include <iostream> #include <set> using namespace std; // Function to display elements of the set // using reverse_iterator void display(set<int> set1) { set<int>::reverse_iterator r_itr; for (r_itr = set1.rbegin(); r_itr != set1.rend(); r_itr++) { cout << *r_itr << ", "; } } // Driver Code int main() { // Create an empty set set<int> set1; // Insert 10 elements into the set set1.insert(70); set1.insert(10); set1.insert(50); set1.insert(90); set1.insert(60); set1.insert(40); set1.insert(100); set1.insert(30); set1.insert(20); set1.insert(80); // Call the display() function display(set1); return 0; } Output100, 90, 80, 70, 60, 50, 40, 30, 20, 10, Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Traverse a Set with reverse_iterator in C++? gottumukkala_sivanagulu Follow Improve Article Tags : C++ Programs C++ STL cpp-set CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Traverse a List with reverse_iterator in C++? In C++, lists are containers that allow users to store data in non-contiguous memory locations. List provides reverse iterators that make it easy to traverse the list in reverse order. In this article, we will learn how to traverse a list with reverse_iterator in C++. Example Input: myList = {10,20, 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 Use a reverse_Iterator with a Map in C++? In C++, a reverse_iterator is a type of iterator that points to elements in a container in the reverse order. This means that using a reverse_iterator, you can traverse a container from end to beginning. In this article, we will learn how to use a reverse_iterator with a map in C++. Example: Input: 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 Vector Using const_reverse_iterator in C++? In C++, a vector can be traversed in reverse order using a const_reverse_iterator. A const_reverse_iterator is a type of iterator that points to the last element in the container and moves in the reverse direction. In this article, we will learn how to traverse a vector using a const_reverse_iterato 2 min read How to Traverse a Map Using Iterator in C++? In C++, maps are associative containers that store elements where each element has a key value and a mapped value. An iterator is a pointer-like object that allows traversing through the elements of a container like a map. In this article, we will learn how to traverse a map using an iterator in C++ 2 min read How to Reverse Iterate a Vector in C++? In this article, we will learn different methods to iterate through the vector in reverse order in C++.The most efficient method to iterate through the vector in reverse order is by using reverse iterator. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int mai 2 min read How to Use Iterator with a Vector in C++? In C++, vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted. An iterator is a pointer-like object that can be used to access elements of a container (like an array or a vector). In this article, we will learn how to use an 2 min read How to Use const_iterator with a Map in C++? In C++, a const_iterator is a type of iterator that points to constant content. This means that using a const_iterator, you can read from but not write to the element it points to. In this article, we will learn how to use a const_iterator with a map in C++ STL. Example: Input: myMap = {{âappleâ, 1} 2 min read Like