English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and example of vector crend()

C++ Vector (Container)

This function is used to point to the reverse iterator of the vector (vector) container, returning a const_iterator pointing to the first element of the container.

Syntax

The syntax of the vector (vector) "v" is:

const_reverse_iterator itr = v.crend();

Parameters

It does not contain any parameters.

Return value

It returns a constant reverse iterator that points to the reverse end of the sequence.

Example1

Let's look at a simple example.

#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5;
vector<int>::const_reverse_iterator itr = v.crend();-2;
  *itr=9;
cout<<*itr;
return 0;
}
//In this example, it indicates that the crend() function does not modify the value, otherwise, it will display an error.

Example2

Let's take another simple example

#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<string>str{"java","C","C++,".Net"};
vector<string>::const_reverse_iterator itr = str.crend()-1;
std::cout<< *itr;
return 0;
}

Output:

java

In this example, the crend() function accesses the first element of the vector (vector) container.

C++ Vector (Container)