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

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

C++ vector::crbegin() function

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

It returns a const_reverse_iterator which is an iterator point to the constant content(vector), the const_reverse_iterator 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 of vector::crbegin() function

    vector::crbegin();

Parameter(s): none – It accepts nothing.

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

Example:

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

    Function call:
    vector<int>::const_iterator crit;
    crit = vector1.crbegin();
    cout<<*crit;

    Output:
    5

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

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

    return 0;
}

Output

last element is: 50

Reference: C++ vector::crbegin()


Related Tutorials



Comments and Discussions!










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