Thursday, 15 July 2010

c++ - How do I remove the first occurrence of a value from a vector? -



c++ - How do I remove the first occurrence of a value from a vector? -

i'm trying single element vector , force of vector remove won't have empty section in memory. erase-remove idiom can removes all instances of particular value. want first 1 removed.

i'm not experienced standard library algorithms , can't find appropriate methods (if any) this. here example:

int main() { std::vector<int> v{1, 2, 3, 3, 4}; remove_first(v, 3); std::cout << v; // 1, 2, 3, 4 }

so how go removing first occurance of 3 vector?

find first, erase it:

auto = std::find(v.begin(),v.end(),3); // check there 3 in our vector if (it != v.end()) { v.erase(it); }

c++ c++11

No comments:

Post a Comment