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

C++ vector rend() Usage and Example

C++ Vector (Container)

rend() function stands for "reverse end", used to return the reverse iterator starting from the vector.

Syntax

The syntax for the vector "v" is:

reverse_iterator ritr = v.rend();

Parameters

It does not contain any parameters.

Return value

Returns the reverse iterator pointing to the starting position of the current vector.

Example1

Let's look at a simple example.

#include#includeusing namespace std;
int main()
{
vector v{1,2,3,4,5};
vector::reverse_iterator ritr;
for(ritr=v.rbegin();ritr!=v.rend();ritr++)
std::cout<< *ritr<<" ";
return 0;
}

Output:

5 4 3 2 1

In this example, the rend() function will be used to get the reverse integer values contained in the vector.

Example2

Let's look at a simple example.

#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<string> v{"Computer science","electronics","electrical","mechanical"};
vector<string>::reverse_iterator ritr;
vector<string>::iterator itr;
std::cout<<"The string is: ";
for(itr=v.begin();itr!=v.end();itr++)
cout <<*itr<<", ";
cout<<'\n';
cout<<"The reversed string is: ";
for(ritr=v.rbegin();ritr!=v.rend();ritr++)
cout <<*ritr << ", ";
return 0;
}

Output:

The string is: Computer science, electronics, mechanical
The reversed string is: mechanical, electrical, electronics, Computer science

In this example, the rend() function will be used to obtain the reversed string value contained in the vector (vector).

C++ Vector (Container)