![]() |
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 1 Traversing 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.
C++ Program to Use a const_reverse_iterator with a VectorC++
Output
Traversing the vector in reverse order: 5 4 3 2 1 Time Complexity: O(N), where N is the number of elements in the vector. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |