vector cend() in C++

cend(): It returns the read-only (constant) iterator that points one past the last element

of the vector.

Syntax:

vector<data_type>::const_iterator it = vecName.cend()

File: stl_vector.h

For Example:

vec = {1,2,3,4}

vec.cend() = > It returns the iterator beyond the last elemet (i.e beyond 4)

So, if we want to print the last element then we can subtract 1 from the iterator and 

print its value

Approach

C++

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

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

    vector<int>::const_iterator it = vec.cend();

    cout << *(it-1<< "\n";

    return 0;
}


No comments:

Post a Comment