Home »
C++ STL
vector::crend() function with example in C++ STL
C++ STL vector::crend() function: Here, we are going to learn about the crend() function of vector header in C++ STL with example.
Submitted by IncludeHelp, on May 11, 2019
C++ vector::crend() function
vector::crend() is a library function of "vector" header, it is used to get the first element of a vector from reverse ending, it returns a const reverse iterator pointing to the element preceding the first element (i.e. reverse ending) of a vector.
It returns a const_reverse_iterator which is an iterator point to the constant content(vector), the const_reverse_itertator can be increased or decreased just like an iterator but it cannot be used to update/modify the vector content it points to.
Note: To use vector, include <vector> header.
Syntax
Syntax of vector::crend() function
vector::crend();
Parameter(s)
none – It accepts nothing.
Return value
const_reverse_iterator – It returns a const reverse iterator pointing to the element preceding the first elements of the vector.
Sample Input and Output
Input:
vector<int> vector1{ 1, 2, 3, 4, 5 };
Function call:
vector<int>::const_iterator crit;
crit = vector1.crend()-1;
cout<<*crit;
Output:
1
C++ program to demonstrate example of vector::crend() function
//C++ STL program to demonstrate example of
//vector::crend() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//creating iterator
vector<int>::const_reverse_iterator crit;
crit = v1.crend()-1;
cout << "first element is: " << *crit << endl;
return 0;
}
Output
first element is: 10
Reference: C++ vector::crend()