Home »
C++ STL
vector::begin() function with example in C++ STL
C++ STL vector::begin() function: Here, we are going to learn about the begin() function of vector header in C++ STL with example.
Submitted by IncludeHelp, on May 09, 2019
C++ vector::begin() function
vector::begin() is a library function of "vector" header, it is used to get the first element of a vector. It returns an iterator pointing to the first element of the vector.
Note: To use vector, include <vector> header.
Syntax
Syntax of vector::begin() function
vector::begin();
Parameter(s)
none – It accepts nothing.
Return value
iterator – It returns an 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>::iterator it;
it = vector1.begin();
cout << *it;
Output:
1
C++ STL program to demonstrate example of vector::begin() function
//C++ STL program to demonstrate example of
//vector::begin() 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>::iterator it;
it = v1.begin();
cout << "first element is: " << *it << endl;
return 0;
}
Output
first element is: 10
Reference: C++ vector::begin()