In std::multiset is there a function or algorithm to erase just one sample (unicate or duplicate) if an element is found

Martin picture Martin · Feb 6, 2012 · Viewed 36.2k times · Source

Perhaps this is a duplicate but I did not find anything searching: When erase(value) is called on std::multiset all elements with the value found are deleted. The only solution I could think of is:

std::multiset<int>::iterator hit(mySet.find(5));
if (hit!= mySet.end()) mySet.erase(hit);

This is ok but I thought there might be better. Any Ideas ?

Answer

user2251346 picture user2251346 · Jul 12, 2016
auto itr = my_multiset.find(value);
if(itr!=my_multiset.end()){
    my_multiset.erase(itr);
}

I would imagine there is a cleaner way of accomplishing the same. But this gets the job done.