0

I wanted to store a pair of elements in a multiset and keep it sorted according to the first value stored in the pair. I made a comparator structure which helped me do this. But now i dont know how to use the multiset's find and erase functions. Is there anyway i can use the multiset's find and erase for multiset of pair?

Aaron
  • 1
  • 1

1 Answers1

1
multiset<int,int> myset;
myset.insert(make_pair(3,5));
if(myset.find(make_pair(3,5))!=myset.end())
    myset.erase(myset.find(make_pair(3,5)));

This will delete a single pair (3,5). If you want to delete all pairs (3,5), you need to simply do myset.erase(make_pair(3,5)) .

std::multiset::erase is overloaded and can take either the value or the iterator/range of iterator. If you pass the iterator, only that element is deleted. But if you pass the value, all elements matching that value will be deleted. If your data type in the multiset is a user-defined object, you need to overload the == operator.

std::multiset::erase