YetiCodeCamp - C++ for Beginners

Companion website for the C++ for Beginners YouTube video series.

C++ Vector begin() Function

The C++ vector.begin() function returns an iterator to the first element of a vector. This function takes no parameters.

If the vector is empty, the returned iterator will be equal to end().



Returns an iterator to the first element of a vector

This example uses the assign function with the first and last parameters to assign the value of another vector (v1) to vector v2. We are using the vector begin and end functions to retrieve the starting and ending elements of vector v1 and using them in the vector assign() parameter list.

std::vector<int> v1 {1,2,3,4,5};
std::vector<int> v2;
v2.assign(v1.begin(), v1.end());
for( v : v2 )
    std::cout << v2 << " ";

Output: 1 2 3 4 5

This example is a complete program using vector the begin() function to print the contents of a vector. We are also using the vector end() function to determine the number of elements in the vector to iterate through.


int main()
  std::vector<int> v1{1,2,3,4,5,6};

  for(std::vector<int>::iterator itr = v1.begin();itr !=v1.end();itr++)
      std::cout << * itr << " ";

  return 0;

Output: 1 2 3 4 5 6