How to Traverse Vector Using const_reverse_iterator in C++? Last Updated : 05 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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_iterator in C++. Example: Input: myVector = {1, 2, 3, 4, 5}; Output: 5 4 3 2 1Traversing a Vector Using const_reverse_iterator in C++To traverse a std::vector using a const_reverse_iterator, we can use the std::vector::crbegin() function to get a constant reverse_iterator pointing to the last element of the vector and the std::vector::crend() function to get a constant reverse_iterator pointing to one position before the first element of the vector. We can then use the loop to increment these iterators to traverse the whole vector. Note: const_reverse_iterator are generally used when we dont want to change the value stored in the vector. C++ Program to Use a const_reverse_iterator with a Vector C++ // C++ program to illustrate how to traverse a vector using // a const_reverse_iterator #include <iostream> #include <vector> using namespace std; int main() { // Creating a vector vector<int> myVector = { 1, 2, 3, 4, 5 }; // Getting a const_reverse_iterator to the beginning of // the reversed vector vector<int>::const_reverse_iterator it = myVector.crbegin(); // Traversing the vector using the // const_reverse_iterator cout << "Traversing the vector in reverse order: "; while (it != myVector.crend()) { cout << *it << " "; ++it; } cout << endl; return 0; } OutputTraversing the vector in reverse order: 5 4 3 2 1 Time Complexity: O(N), where N is the number of elements in the vector.Auxiliary Sapce : O(1) Comment More infoAdvertise with us Next Article How to Traverse a Set with const_iterator in C++? H heysaiyad Follow Improve Article Tags : C++ Programs C++ cpp-iterator STL cpp-vector CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Traverse Vector using const_iterator in C++? In C++, a const_iterator is a type of iterator that points to constant content means by using a const_iterator, we can read from but cannot write to the element it points to. In this article, we will learn how to use a const_iterator with a vector in C++ STL. Example: Input: myVector = {1, 2, 3} Out 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 Traverse a Set with reverse_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 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_iter 2 min read 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 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 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 Like