Home »
C++ STL
vector::cbegin() function with example in C++ STL
C++ STL vector::cbegin() function: Here, we are going to learn about the cbegin() function of vector header in C++ STL with example.
Submitted by IncludeHelp, on May 11, 2019
C++ vector::cbegin() function
vector::cbegin() is a library function of "vector" header, it is used to get the const iterator pointing to the first 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.
Note: To use vector, include <vector> header.
Syntax
Syntax of vector::cbegin() function
vector::cbegin();
Parameter(s)
none – It accepts nothing.
Return value
const_iterator – It returns a constant iterator pointing to the first element of the vector.
Sample Input and Output
Input:
vector<int> vector1{ 1, 2, 3, 4, 5 };
Function call:
vector<int>::const_iterator cit;
cit = vector1.cbegin();
cout<<*cit;
Output:
1
C++ program to demonstrate example of vector::cbegin() function
//C++ STL program to demonstrate example of
//vector::cbegin() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//creating iterator
vector<int>::const_iterator cit;
cit = v1.cbegin();
cout << "first element is: " << *cit << endl;
return 0;
}
Output
first element is: 10
Reference: C++ vector::cbegin()