This post will discuss how to remove from the beginning of a vector in C++.

1. Using vector::erase

The recommended solution to remove an element from a vector is using the vector::erase function. It takes an iterator to the element to be deleted. To remove an element from the beginning of a vector, pass an iterator to the first element in the vector. This is demonstrated below:

Download  Run Code

Output:

2 3 4

2. Using std::deque

A std::deque is preferred to add or remove elements from either end of a container. It implements a Double-ended queue that expands or shrinks on both ends. To remove an element from the beginning of the vector, use the pop_front member function of std::deque.

Download  Run Code

Output:

2 3 4

That’s all about removing from the beginning of a vector in C++.