English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ vector cend() usage and example

C++ Vector (Container)

This function is used to point to the last element in the vector (vector).

cend() and end()

cend() returnsa constant iteratorwhile the end() function returnsIterator.end() functionthe element pointed tocan be modifiedButcend() functionCannotModification.

Syntax

Consider the vector (vector) 'v', the syntax is:

const_iterator itr = v.cend();

Parameters

It does not contain any parameters.

Return value

It returns a constant iterator pointing to the last element in the vector (vector).

Example1

Let's look at a simple example.

#include iostream
#include<vector>
using namespace std;
int main()
{
  vector<char> v{'T','u','t','o','r','i','a','l'};
vector<char>::const_iterator citr;
for(citr = v.cbegin(); citr != v.cend(); citr++)
std::cout <<*citr;
return 0;
}

Output:

Tutorial

In this example, the cend() function is accessed using an object of the constant iterator type.

Example2

Let's look at a simple example.

#include iostream
#include<vector>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5};
vector<int>::const_iterator citr;
for(citr = v.cbegin(); citr != v.cend(); citr++)
std::cout <<*citr << " ";
return 0;
}

Output:

1 2 3 4 5

C++ Vector (Container)