In this post, we’ll see how to remove the range of elements from a vector between specified indices in C++.

We can use the public member function std::vector::erase of the container std::vector for removing a part of the vector between the specified indices.

 
We have two overloaded versions of the erase() function: the first one removes only a single element at the specified position, and the second one removes all elements between the specified range. The solution is feasible using either one of them:

1. Using erase(const_iterator pos) function

Download  Run Code

Output:

1 2 3 4 5

2. Using erase(const_iterator first, const_iterator last) function

Download  Run Code

Output:

1 2 3 4 5

That’s all about removing a range of elements from a vector in C++.