Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove only one item from unordered_multiset

I want to erase a specific element from std::unordered_multiset, but when I try with erase function, it eliminates all the items, no matter how many they are.
For example:

std::unordered_multiset<int> M;
M.insert(1);
M.insert(1);
M.insert(1);
std::cout<<M.count(1)<<std::endl;

M.erase(1);
std::cout << M.count(1) << std::endl;

I expect this to print 3 then 2. But it prints 3 then 0. So how to remove only one item?

like image 987
Eduard Rostomyan Avatar asked Sep 08 '25 01:09

Eduard Rostomyan


1 Answers

You can use another erase overload:

std::unordered_multiset<int> s { 1, 2, 2, 3, 3, 3 };

const auto it = s.find(2);

if (it != s.end())
    s.erase(it);

Live version

like image 111
Bartek Banachewicz Avatar answered Sep 10 '25 05:09

Bartek Banachewicz