std::multiset<int> myMultiSet = {1, 2, 2, 3, 4, 4, 4, 5};
// Find an element to remove
int valueToRemove = 4;
auto it = myMultiSet.find(valueToRemove);
// Check if the element is found before erasing
if (it != myMultiSet.end()) {
myMultiSet.erase(it);
std::cout << "Removed one instance of " << valueToRemove << std::endl;
} else {
std::cout << "Element not found" << std::endl;
}
If you use erase(theNumber)
on a std::multiset
, it will remove all instances of the specified value from the multiset. This is because std::multiset
allows duplicate values, and the erase
function removes all elements equal to the given value.