Write a program that defines a vector of integers.
Erase the range of 3 elements starting from the beginning of the vector.
Print out the vector content using the range-based loop.
You can use the following code structure:
#include <iostream> int main() { }
#include <iostream> #include <vector> int main() { std::vector<int> v = { 10, 5, 8, 4, 1, 2 }; v.erase(v.begin(), v.begin() + 3); // erase the first 3 elements for (auto el : v) { std::cout << el << '\n'; } }
In this case the erase()
function overload accepts two arguments.
One is the beginning of the range to be deleted.
In our case, it is marked with v.begin()
.
The second argument is the end of the range to be deleted.
In our case, it is the v.begin()
+ 3 iterator.
Instead of .begin()
member function we could have used a freestanding std::begin(v) function.