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

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

C++ vector::rbegin() function

vector::rbegin() is a library function of "vector" header, it is used to get the last element of a vector using reverse_iterator, it returns a reverse iterator pointing to the last elements (i.e. the reverse beginning) of a vector.

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

Syntax of vector::rbegin() function

    vector::rbegin();

Parameter(s): none – It accepts nothing.

Return value: iterator – It returns a reverse iterator pointing to the last element of the vector.

Example:

    Input:
    vector<int> vector1{ 1, 2, 3, 4, 5 };

    Function call:
    vector<int>::reverse_iterator rit;
    rit = vector1.rbegin();
    cout << *rit;

    Output:
    5

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

//C++ STL program to demonstrate example of 
//vector::rbegin() 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.rbegin();
    cout << "last element is: " << *rit << endl;

    return 0;
}

Output

last element is: 50

Reference: C++ vector::rbegin()


Related Tutorials



Comments and Discussions!

Load comments ↻





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