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:
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.
#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 instance, the cend function is invoked using an object of const_iterator type.
Example 2
Let's see 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