This post will discuss how to iterate from the second element of a vector in C++. Assume the vector is not empty.

1. Using std::advance function

A simple solution is to get an iterator to the beginning of the vector and use the STL algorithm std::advance to advance the iterator by one position. Then we use that iterator as the starting position in the loop.

Download  Run Code

Output:

2 3 4 5

2. Using std::next or ++, + operator

We can even set the starting iterator in a single line using any of the following methods:

⮚ Using std::next in C++11

Download  Run Code

⮚ Using ++ operator

Download  Run Code

⮚ Using + operator

Download  Run Code

3. Skipping the first element inside the loop

The idea is to use a normal for-loop to iterate the vector and skip the first element inside the loop, as shown below:

Download  Run Code

That’s all about iterating from the second element of a vector in C++.