These functions return useful iterators to access vector elements in reverse order (from end to beginning)
Using vector::crend()
It’s a public member function that returns a const_reverse_iterator pointing to the element preceding the first element.
Return Value
A const_reverse_iterator to the reverse end of the sequence.
Syntax:
const_reverse_iterator crend() const noexcept;
Using vector::crbegin()
It returns a const_reverse_iterator pointing to the last element in the container (i.e., its reverse beginning)..
Return Value
A const_reverse_iterator to the reverse beginning of the sequence.
Syntax:
const_reverse_iterator crbegin() const noexcept;
// CPP program to illustrate working of crbegin() // crend() #include <iostream> #include <vector> using namespace std; int main () { // initializing vector with values vector<int> vect = {10, 20, 30, 40, 50}; // for loop with crbegin and crend for (auto i = vect.crbegin(); i != vect.crend(); i++) cout << ' ' << *i; //printing results cout << '\n'; return 0; } |
Output:
50 40 30 20 10
Applications
crend :
Returns a reverse iterator to the element following the last element of the reversed container. It corresponds to the element preceding the first element of the non-reversed container. This element acts as a placeholder, attempting to access it results in undefined behavior
crbegin :
Returns a reverse iterator to the first element of the reversed container. It corresponds to the last element of the non-reversed container.
Both are used in deque operations, concurrently accessing or modifying different elements is safe.
The copy construction or assignment of the returned iterator is also guaranteed to never throw exception.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to iterate over the elements of an std::tuple in C++
- How to access private/protected method outside a class in C++
- lvalues references and rvalues references in C++ with Examples
- Difference between cout and std::cout in C++
- Sorting Vector of Arrays in C++
- C++ program to check whether a String is a Pangram or not
- Difference between pair in Multiset and Multimap in C++ STL
- How to use unordered_map efficiently in C++
- Header files in C/C++ with Examples
- C++ Program for the Fractional Knapsack Problem
- Minimize count of divisions by D to obtain at least K equal array elements
- Trigraphs in C++ with Examples
- Difference Between DART and C++
- How to fix auto keyword error in Dev-C++
- Lexicographical smallest number after at most K consecutive swaps
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Anish Kumar Arya

