Vector Cend Function - C++ Programming Tutorial
C++ Course / STL Vector / Vector Cend Function

Vector Cend Function

BLUF: Mastering Vector Cend Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Vector Cend Function

C++ is renowned for its efficiency. Learn how Vector Cend Function enables low-level control and high-performance computing in the tutorial below.

C++ Vector cend

This function is utilized to reference the element beyond the last element in the vector.

cend vs end

The cend function provides a constant iterator, whereas the end function provides a regular iterator. Elements referenced by the end function in C++ tutorials can be altered, unlike those referenced by the cend function.

Syntax

Consider a vector 'v', Syntax would be:

Example

const_iterator itr=v.cend();

Parameter

It does not contain any parameter.

Return value

It provides a fixed iterator pointing to the element one position beyond the final element in the vector.

Example 1

Let's see a simple example.

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:

Output

Tutorial

In this instance, the cend function is invoked using an object of const_iterator type.

Example 2

Let's see a simple example.

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:

Output

1 2 3 4 5

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience