vector crend() in C++

crend(): It is an in-build function in STL of C++. It returns the read-only (constant) reverse iterator that points to one before the first element of the given vector.

Note : Iteration is done in reverse element order.

Syntax:

vector<data_type>::const_reverse_iterator it = vecName.crend()

File: stl_vector.h

For Example: 

vec = {1,2,3,4,5}

vec.crend() => It points before 1.

So to print the first elemet we go subtract 1 from it. We subtract because iteration is done

in reverse order

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<intvec = {12345};

    vector<int>::const_reverse_iterator it = vec.crend();
    cout << *(it-1<< "\n";
    

    return 0;
}


No comments:

Post a Comment