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.
Example – Remove or Delete 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
Conclusion
In this C++ Tutorial, we learned how to remove the first element of the vector.