38

How do I remove the ith item from a std::vector?

I know I want to delete the ith element. I have int i; and std::vector<process> pList; where process is a struct. I want to do something equivalent to the following:

pList.remove(i); 
0

5 Answers 5

90

Here is an O(1) solution, assuming you don't care about the order of elements:

#include <algorithm> // ... { using std::swap; swap(pList[i], pList.back()); pList.pop_back(); } 

For PODs, assignment is faster than swapping, so you should simply write:

pList[i] = pList.back(); pList.pop_back(); 

In C++11, you can forget the above distinction and always use move semantics for maximum efficiency:

if (i != pList.size() - 1) { // Beware of move assignment to self // see http://stackoverflow.com/questions/13127455/ pList[i] = std::move(pList.back()); } pList.pop_back(); 
Sign up to request clarification or add additional context in comments.

5 Comments

That's clever assuming order is not important.
An even faster version would skip the swap and instead just assign, as the last element is getting pop_backed anyway.
@Ylisar: For many types, swapping is much more efficient than assignment. Examples include most of the STL containers. Assignment is more efficient for PODs though. Thanks for reminding me.
Is this algorithm implemented somewhere in std or boost?
This is what I always do, it works like a charm!
64
pList.erase(pList.begin()+i); 

To remove element with index i.

3 Comments

Thanks! I didn't understand how to get an iterator to pass to the method...
one note, the behavior is undefined if the vector contains less than i+1 elements. It's probably worth noting :)
Note that this will preserve the order of elements, and is O(n). If you don't need to preserve order, use the O(1) solution mentioned by others.
15

An approach to save yourself from linear complexity!

Since vector.erase() is linear complexity, I would suggest that just swap the ith element with the last element and then erase the element at the end (which is actually ith element); that way, you possibly can save yourself from linear complexity. That is just my thought!

1 Comment

"vector.erase() is linear complexity" Is there a book with a tabler for such things? I keep getting an impression that such estimates need a thorough knowledge of some algorithms (but I still hope there is a table with methods complexity for each data type somewhere). Thanks.
5
vector.erase(iterator) 

Where iterator is the position. You can get the first element with vector.begin() and the last one with vector.end(). Just add to the iterator to get to the desired element. e.g.:

pList.erase(pList.begin()+6); 

to erase the 6th item.

1 Comment

it is the 7th item, isn't it? cplusplus.com
4

Use Vector.Erase. The complexity is linear on the number of elements erased (destructors) plus the number of elements after the last element deleted (moving).

iterator erase ( iterator position ); iterator erase ( iterator first, iterator last ); 

1 Comment

Yes i saw this in the documentation but was still unable to figure out how to use it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.