pop a specific element off a vector in c++
55,198
Solution 1
Assuming you're looking for the element containing the value 2
, not the value at index 2
.
#include<vector>
#include<algorithm>
int main(){
std::vector<int> a={1,2,3};
a.erase(std::find(a.begin(),a.end(),2));
}
(I used C++0x to avoid some boilerplate, but the actual use of std::find
and vector::erase
doesn't require C++0x)
Solution 2
//erase the i-th element
myvector.erase (myvector.begin() + i);
(Counting the first element in the vector as as i=0
)
Solution 3
Also, remember to use the erase-remove idiom if you are removing multiple elements.
Solution 4
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
unsigned int i;
vector<unsigned int> myvector;
// set some values (from 1 to 10)
for (i=1; i<=10; i++) myvector.push_back(i);
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
cout << "myvector contains:";
for (i=0; i<myvector.size(); i++)
cout << " " << myvector[i];
cout << endl;
return 0;
}
Related videos on Youtube

Author by
kamikaze_pilot
Updated on April 24, 2020Comments
-
kamikaze_pilot about 3 years
so suppose I have a vector called v and it has three elements: 1,2,3
is there a way to specifically pop 2 from the vector so the resulting vector becomes
1,3
-
karlphillip about 12 yearsYou copied that code from some place. Where is it from? --> cplusplus.com/reference/stl/vector/erase
-
karlphillip about 12 yearsThat's a bad answer, Christo. Check what @Todd did with 2 lines only.
-
kamikaze_pilot about 12 yearsyeah but what if you want to delete by element not position....so for instance in python you can pop a list pop(x) if that element in the list is x regardless of its position
-
Xeo about 12 years@kamikaze: Worst case of course
O(n)
, aka it needs to search through the whole vector. If you have a sorted vector, you can trylower_bound
-
Benjamin Lindley about 12 years@kamikaze_pilot: Are you sure you can do that with a python list?
-
Ken Bloom about 12 yearsIf finding elements by value is a common operation on your vector, you should be using a
std::set
orstd::unsorted_set
instead. -
Jerry Coffin about 12 years@Karlphillip: Perhaps it would help to read the code a bit more carefully -- this is a complete, compilable program. The erasing is only a couple of lines, about the same amount as @Todd's answer. What this adds is intializing, printing out the result, etc.
-
karlphillip about 12 years@Jerry Maybe I exaggerated on my comment. But I still don't think that throwing code without an explanation is a good answer, specially when the code was copied from somebody else. This was a copy/paste answer without reference to the author.
-
Jerry Coffin about 12 years@Karlphillip: Well, I'd certainly agree with that -- just posting a chunk of code with no explanation is rarely very helpful. Failing to attribute the code doesn't (IMO) affect its quality as an answer, but is discourteous if you can avoid it. I've done it a few times with code I'd had lying around for a long time, and lost track of where it came from, but never really intentionally.
-
karlphillip about 12 years@Jerry I enjoy your blog, by the way. You should post more often. =)
-
Jerry Coffin about 12 years@karlphillip: I was thinking of that last night, but didn't get a chance. I'm glad to hear somebody's getting something out of it -- it's a bit hard to guess whether anybody's even noticed that it exists. I'll see what I can do about posting a bit more often...
-
Christo about 12 years@karlphilip: yeah, I should have put the URL up there, but it's the C++ STL doco, and I figured if he didn't find erase() maybe he needed something a little more full.
-
Ken Bloom about 12 yearsIn Python
pop
deletes an item by index (Todd's answer), andremove
deletes an item by value (my answer).