Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

stl

multiset

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 ?

like image 913
Martin Avatar asked Feb 06 '12 21:02

Martin


People also ask

How do you clear a multiset in C++?

The multiset::clear() function is a built-in function in C++ STL which removes all elements from the multiset container. The final size of multiset container after removal is 0.

Does multiset keep elements in sorted order?

Multiset: They are associative containers that store multiple elements having equivalent values following a specific order. Following are the properties of multisets: Stores elements in sorted order. It allows the storage of multiple elements.


2 Answers

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.

like image 195
user2251346 Avatar answered Sep 20 '22 17:09

user2251346


Try this one:

multiset<int> s; s.erase(s.lower_bound(value)); 

As long as you can ensure that the value exists in the set. That works.

like image 23
Strange Avatar answered Sep 19 '22 17:09

Strange