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
//erase the i-th element myvector.erase (myvector.begin() + i);
(Counting the first element in the vector as as i=0
)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With