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;
}
Share:
55,198

Related videos on Youtube

kamikaze_pilot
Author by

kamikaze_pilot

Updated on April 24, 2020

Comments

  • kamikaze_pilot
    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
    karlphillip about 12 years
    You copied that code from some place. Where is it from? --> cplusplus.com/reference/stl/vector/erase
  • karlphillip
    karlphillip about 12 years
    That's a bad answer, Christo. Check what @Todd did with 2 lines only.
  • kamikaze_pilot
    kamikaze_pilot about 12 years
    yeah 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
    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 try lower_bound
  • Benjamin Lindley
    Benjamin Lindley about 12 years
    @kamikaze_pilot: Are you sure you can do that with a python list?
  • Ken Bloom
    Ken Bloom about 12 years
    If finding elements by value is a common operation on your vector, you should be using a std::set or std::unsorted_set instead.
  • Jerry Coffin
    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
    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
    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
    karlphillip about 12 years
    @Jerry I enjoy your blog, by the way. You should post more often. =)
  • Jerry Coffin
    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
    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
    Ken Bloom about 12 years
    In Python pop deletes an item by index (Todd's answer), and remove deletes an item by value (my answer).

Related