How do I remove an item from a stl vector with a certain value?


I was looking at the API documentation for stl vector, and noticed there was no method on the vector class that allowed the removal of an element with a certain value. This seems like a common operation, and it seems odd that there's no built in way to do this.

This question is tagged with c++ stl

~ Asked on 2008-09-02 16:14:13

11 Answers


std::remove does not actually erase the element from the container, but it does return the new end iterator which can be passed to container_type::erase to do the REAL removal of the extra elements that are now at the end of the container:

std::vector<int> vec;
// .. put in some values ..
int int_to_remove = n;
vec.erase(std::remove(vec.begin(), vec.end(), int_to_remove), vec.end());

~ Answered on 2008-09-02 16:23:15


If you want to remove an item, the following will be a bit more efficient.

std::vector<int> v;

auto it = std::find(v.begin(), v.end(), 5);
if(it != v.end())

or you may avoid overhead of moving the items if the order does not matter to you:

std::vector<int> v;

auto it = std::find(v.begin(), v.end(), 5);

if (it != v.end()) {
  using std::swap;

  // swap the one to be removed with the last element
  // and remove the item at the end of the container
  // to prevent moving all items after '5' by one
  swap(*it, v.back());

~ Answered on 2013-04-14 11:52:58


Use the global method std::remove with the begin and end iterator, and then use std::vector.erase to actually remove the elements.

Documentation links

std::vector<int> v;

//Vector should contain the elements 1, 2

//Find new end iterator
std::vector<int>::iterator newEnd = std::remove(v.begin(), v.end(), 1);

//Erase the "removed" elements.
v.erase(newEnd, v.end());

//Vector should now only contain 2

Thanks to Jim Buck for pointing out my error.

~ Answered on 2008-09-02 16:16:19


The other answers cover how to do this well, but I thought I'd also point out that it's not really odd that this isn't in the vector API: it's inefficient, linear search through the vector for the value, followed by a bunch of copying to remove it.

If you're doing this operation intensively, it can be worth considering std::set instead for this reason.

~ Answered on 2008-09-02 16:49:36


If you have an unsorted vector, then you can simply swap with the last vector element then resize().

With an ordered container, you'll be best off with ?std::vector::erase(). Note that there is a std::remove() defined in <algorithm>, but that doesn't actually do the erasing. (Read the documentation carefully).

~ Answered on 2008-09-02 16:37:03


From c++20:

A non-member function introduced std::erase, which takes the vector and value to be removed as inputs.


std::vector<int> v = {90,80,70,60,50};

~ Answered on 2019-05-15 20:36:41


A shorter solution (which doesn't force you to repeat the vector name 4 times) would be to use Boost:

#include <boost/range/algorithm_ext/erase.hpp>

// ...

boost::remove_erase(vec, int_to_remove);


~ Answered on 2017-07-19 09:17:09


See also std::remove_if to be able to use a predicate...

Here's the example from the link above:

vector<int> V;

copy(V.begin(), V.end(), ostream_iterator<int>(cout, " "));
    // The output is "1 4 2 8 5 7"

vector<int>::iterator new_end = 
    remove_if(V.begin(), V.end(), 
              compose1(bind2nd(equal_to<int>(), 0),
                       bind2nd(modulus<int>(), 2)));
V.erase(new_end, V.end()); [1]

copy(V.begin(), V.end(), ostream_iterator<int>(cout, " "));
    // The output is "1 5 7".

~ Answered on 2008-09-02 16:22:24



C++ community has heard your request :)


C++ 20 provides an easy way of doing it now. It gets as simple as :

#include <vector>
vector<int> cnt{5, 0, 2, 8, 0, 7};
std::erase(cnt, 0);

You should check out std::erase and std::erase_if.

Not only will it remove all elements of the value (here '0'), it will do it in O(n) time complexity. Which is the very best you can get.

If your compiler does not support C++ 20, you should use erase-remove idiom:

#include <algorithm>
vec.erase(std::remove(vec.begin(), vec.end(), 0), vec.end());

~ Answered on 2020-05-04 07:47:13


Two ways are there by which you can use to erase an item particularly. lets take a vector

std :: vector < int > v;

1) Non efficient way : Although it seems to be quite efficient but it's not because erase function delets the elements and shifts all the elements towards left by 1. so its complexity will be O(n^2)

std :: vector < int > :: iterator itr = v.begin();
int value = 40;
while ( itr != v.end() )
   if(*itr == value)

2) Efficient way ( RECOMMENDED ) : It is also known as ERASE - REMOVE idioms .

  • std::remove transforms the given range into a range with all the elements that compare not equal to given element shifted to the start of the container.
  • So, actually don't remove the matched elements. It just shifted the non matched to starting and gives an iterator to new valid end. It just requires O(n) complexity.

output of the remove algorithm is :

10 20 30 50 40 50 

as return type of remove is iterator to the new end of that range.

template <class ForwardIterator, class T>
  ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);

Now use vector’s erase function to delete elements from the new end to old end of the vector. It requires O(1) time.

v.erase ( std :: remove (v.begin() , v.end() , element ) , v.end () );

so this method work in O(n)

~ Answered on 2018-10-03 09:30:03


If you want to do it without any extra includes:

vector<IComponent*> myComponents; //assume it has items in it already.
void RemoveComponent(IComponent* componentToRemove)
    IComponent* juggler;

    if (componentToRemove != NULL)
        for (int currComponentIndex = 0; currComponentIndex < myComponents.size(); currComponentIndex++)
            if (componentToRemove == myComponents[currComponentIndex])
                //Since we don't care about order, swap with the last element, then delete it.
                juggler = myComponents[currComponentIndex];
                myComponents[currComponentIndex] = myComponents[myComponents.size() - 1];
                myComponents[myComponents.size() - 1] = juggler;

                //Remove it from memory and let the vector know too.
                delete juggler;

~ Answered on 2017-12-02 01:21:08

Most Viewed Questions: