pop a specific element off a vector in c++
Asked Answered
O

5

26

Suppose I have a vector 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}.

Olney answered 24/4, 2011 at 2:38 Comment(0)
S
28

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)

Stringed answered 24/4, 2011 at 2:48 Comment(2)
@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_boundBe
If finding elements by value is a common operation on your vector, you should be using a std::set or std::unsorted_set instead.Stringed
L
44
//erase the i-th element
myvector.erase (myvector.begin() + i);

(Counting the first element in the vector as as i=0)

Luxate answered 24/4, 2011 at 2:42 Comment(3)
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 positionOlney
@kamikaze_pilot: Are you sure you can do that with a python list?Reentry
In Python pop deletes an item by index (Todd's answer), and remove deletes an item by value (my answer).Stringed
S
28

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)

Stringed answered 24/4, 2011 at 2:48 Comment(2)
@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_boundBe
If finding elements by value is a common operation on your vector, you should be using a std::set or std::unsorted_set instead.Stringed
B
10

Also, remember to use the erase-remove idiom if you are removing multiple elements.

Bobseine answered 24/4, 2011 at 2:59 Comment(0)
G
1
#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;
}
Gopher answered 24/4, 2011 at 2:41 Comment(8)
You copied that code from some place. Where is it from? --> cplusplus.com/reference/stl/vector/eraseReis
That's a bad answer, Christo. Check what @Todd did with 2 lines only.Reis
@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.Sinotibetan
@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.Reis
@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.Sinotibetan
@Jerry I enjoy your blog, by the way. You should post more often. =)Reis
@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...Sinotibetan
@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.Gopher
H
0

From C++ 20, we can use std::erase for removing multiple elements.

(no need for "erase-remove idiom")

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v{1,2,2,3,4,5};
      
    std::erase(v, 2);
    
    for(auto e : v) std::cout << e << " ";
    std::cout << '\n';

    // output: 1 3 4 5
}
Hypnotist answered 26/9, 2022 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.