×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

vector::rend() function with example in C++ STL

C++ STL vector::rend() function: Here, we are going to learn about the rend() function of vector header in C++ STL with example.
Submitted by IncludeHelp, on May 09, 2019

C++ vector::rend() function

vector::rend() is a library function of "vector" header, it is used to get the first element of a vector using reverse_iterator, it returns a reverse iterator pointing to the element preceding the first element (i.e. reverse ending) of a vector.

Note: To use vector, include <vector> header.

Syntax

Syntax of vector::rend() function

vector::rend();

Parameter(s)

none – It accepts nothing.

Return value

iterator – It returns an 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>::reverse_iterator rit;
rit = vector1.rend()-1;
cout << *rit;

Output:
1

C++ program to demonstrate example of vector::rend() function

//C++ STL program to demonstrate example of 
//vector::rend() 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>::reverse_iterator rit;
    rit = v1.rend()-1;
    cout << "first element is: " << *rit << endl;

    return 0;
}

Output

first element is: 10

Reference: C++ vector::rend()

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.