![]() |
VOOZH | about |
In C++, the vector crend() is a built-in method used to obtain a constant reverse iterator pointing to the theoretical element just before the first element of the vector. It is used to mark the reverse end of the vector.
Let’s take a look at an example:
4 2 5 3 1
This article covers the syntax, usage, and common examples of the vector crend() method in C++:
Table of Content
The vector crend() is a member method of the std::vector class defined inside the <vector> header file.
v.crend();
Parameters:
Return Value:
The vector crend() function returns a constant reverse iterator of type vector::const_reverse_iterator. Since it is a reverse random access iterator, it supports reverse traversal and arithmetic operations such as incrementing, decrementing, and adding/subtracting integers, subtraction of another iterator of same container, comparison.
However, as it is a constant iterator, you cannot modify the elements it points to. Doing so results in a compilation error.
This method is typically used with vector crbegin() to iterate over the vector in reverse without modifying its elements. The below example shows some use cases of vector crend():
4 2 5 3 1
5