Home » C++ STL

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

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

C++ vector::cend() function

vector::cend() is a library function of "vector" header, it can be used to get the last element of a vector. It returns a const iterator pointing to the past-the-end element of the vector.

It returns a const_iterator which is an iterator point to the constant content(vector), the const_itertator can be increased or decreased just like an iterator but it cannot be used to update/modify the vector content it points to.


  • To use vector, include <vector> header.
  • It does not point to the last element, thus to get the last element we can use vector::cend()-1.

Syntax of vector::cend() function


Parameter(s): none – It accepts nothing.

Return value: const_iterator – It returns a const iterator pointing to the past-the-end element of the vector.


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

    Function call:
    vector<int>::const_iterator cit;
    cit = vector1.cend()-1;


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

//C++ STL program to demonstrate example of 
//vector::cend() function

#include <iostream>
#include <vector>
using namespace std;

int main()
    vector<int> v1;


    //creating iterator
    vector<int>::const_iterator cit;
    cit = v1.cend()-1;
    cout << "last element is: " << *cit << endl;

    return 0;


last element is: 50

Reference: C++ vector::cend()

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.