In this C++ tutorial, you will learn how to remove the first element from a vector using vector::erase() function, with example program.
C++ Vector – Remove First Element
To remove first element of a vector, you can use erase() function. Pass iterator to first element of the vector as argument to erase() function.
The iterator returned by begin() points to the first element, so the usual expression for removing the first element is vector.erase(vector.begin()).
Syntax to Remove the First Element from a C++ Vector
vector_name.erase(vector_name.begin());
Here, vector_name.begin() returns an iterator to the first element. The erase() function removes the element at that iterator position.
Examples
1. Remove first element of vector
In the following example, we have defined a vector and initialized with some values. We shall use erase() to remove the first element.
C++ Program
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums;
nums.push_back(6);
nums.push_back(2);
nums.push_back(7);
nums.push_back(1);
nums.erase(nums.begin());
for (int num: nums)
cout << num << endl;
}
Output
2
7
1
The first element, 6, is removed. The elements that followed it move toward the beginning of the vector, leaving 2, 7, and 1.
2. Check for an Empty Vector Before Removing the First Element
Calling erase(begin()) is valid only when the vector contains at least one element. If the vector could be empty, check empty() before erasing.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums;
if (!nums.empty()) {
nums.erase(nums.begin());
} else {
cout << "Vector is empty";
}
return 0;
}
Output
Vector is empty
For an empty vector, begin() is the same as end(), and passing that iterator as the position to erase is not valid.
3. Store the First Vector Element Before Erasing It
If you need the value being removed, read the first element before calling erase(). You can access it with front().
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums = {10, 20, 30};
if (!nums.empty()) {
int removed = nums.front();
nums.erase(nums.begin());
cout << "Removed: " << removed << '\n';
cout << "New first element: " << nums.front();
}
return 0;
}
Output
Removed: 10
New first element: 20
What Happens When erase(begin()) Removes the First Vector Element
A std::vector stores its elements contiguously. When the first element is erased, every remaining element after it has to move one position toward the beginning to close the gap.
- The element at index
0is removed. - The vector’s
size()decreases by one. - The remaining elements shift toward index
0. - Iterators and references at or after the erased position are invalidated.
- The vector’s capacity is not reduced merely by calling
erase().
Time Complexity of Removing the First Element from std::vector
Removing the first element of a vector with erase(begin()) has linear time complexity, O(n), because the remaining elements must be shifted.
For example, after removing the first value from {10, 20, 30, 40}, the values 20, 30, and 40 all move one position toward the front.
Removing the First Vector Element vs Removing the Last Element
Removing the first and last elements of a vector have different costs. A vector provides pop_back() for removing the final element, but it does not provide a pop_front() member function.
| Operation | C++ expression | Typical complexity |
|---|---|---|
| Remove first element | nums.erase(nums.begin()) | O(n) |
| Remove last element | nums.pop_back() | O(1) |
pop_back() is faster for the last element because no remaining vector elements need to be shifted.
Why std::vector Has No pop_front()
std::vector is designed around contiguous storage and efficient access by index. Removing from the front requires shifting all later elements, so the container does not provide a dedicated pop_front() operation.
If your program frequently inserts or removes elements at the front, consider whether a container such as std::deque, which provides pop_front(), better matches that access pattern.
Remove a Vector Element by Index Instead of Always Removing the First
The same erase() function can remove an element at another index. Add the index to begin() to obtain an iterator to that position.
vector_name.erase(vector_name.begin() + index);
For example, nums.erase(nums.begin() + 2) removes the element at index 2. The index must refer to an existing element.
Common Mistakes When Removing the First Element of a C++ Vector
- Calling erase(begin()) on an empty vector: check
empty()first when the vector may contain no elements. - Looking for vector::pop_front():
std::vectordoes not provide this member function; useerase(begin()). - Assuming front removal is constant time: the remaining vector elements must be shifted, making the operation linear.
- Using invalidated iterators: iterators and references at or after the erased position should not be reused after the call.
- Using erase() when you only need to remove the last element: use
pop_back()for that case.
C++ Vector Remove First Element Summary
In this C++ Tutorial, we learned how to remove the first element of the vector.
Use vector.erase(vector.begin()) to remove the first element from a non-empty C++ vector. The vector’s size decreases by one, the remaining elements shift toward the front, and the operation takes linear time in the number of elements that must be moved.
TutorialKart.com