c++ - How to remove element from vector by value but only single instance of that value? -


Here's the problem,

I have a vector and I need to remove 2 elements from the vector Is one after the other. I know the value of those two elements and the indicator of those elements.

At first I tried to use the index of those two elements

  v.erase (v.begin () + index1); V.erase (v.begin () + index2);  

But in the problem array changes the first element of the second element is after the index is erased.

Therefore, I tried to remove them from the value

  v.erase (delete (v.begin (), v.end (), value), v End ());  

but it removes all instances of that value (for example, if 2 is "1" then both will remove it), while I only want to remove one element.

How to solve this?

You can use:

  v.erase (v .begin () + std :: max (index1, index2)); V.erase (v.begin () + std :: min (index1, index2));  

or even

  auto index = std :: minmax (index1, index2); V.erase (v.begin (+ + index.second); V.erase (v.begin (+) + indexed first);  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -