#include <iostream>
#include <map>
void printMultimap(const std::multimap<int, int>& multiMap)
{
std::cout << "MultiMap:\n";
for (const auto& pair : multiMap)
{
std::cout << pair.first << ":" << pair.second << "\n";
}
}
int main()
{
std::multimap<int, int> multiMap;
multiMap.insert(std::make_pair(1, 1));
multiMap.insert(std::make_pair(1, 2));
multiMap.insert(std::make_pair(2, 3));
multiMap.insert(std::make_pair(2, 4));
multiMap.insert(std::make_pair(2, 5));
multiMap.insert(std::make_pair(3, 1));
printMultimap(multiMap);
auto range = multiMap.equal_range(2);
for (auto iterator = range.first; iterator != range.second;)
{
if (iterator->second != 4)
{
multiMap.insert(std::make_pair(-1, iterator->second));
}
iterator = multiMap.erase(iterator);
}
printMultimap(multiMap);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgoKdm9pZCBwcmludE11bHRpbWFwKGNvbnN0IHN0ZDo6bXVsdGltYXA8aW50LCBpbnQ+JiBtdWx0aU1hcCkKewogICAgc3RkOjpjb3V0IDw8ICJNdWx0aU1hcDpcbiI7Cglmb3IgKGNvbnN0IGF1dG8mIHBhaXIgOiBtdWx0aU1hcCkKCXsKCQlzdGQ6OmNvdXQgPDwgcGFpci5maXJzdCA8PCAiOiIgPDwgcGFpci5zZWNvbmQgPDwgIlxuIjsKCX0KfQoKaW50IG1haW4oKQp7CglzdGQ6Om11bHRpbWFwPGludCwgaW50PiBtdWx0aU1hcDsKCW11bHRpTWFwLmluc2VydChzdGQ6Om1ha2VfcGFpcigxLCAxKSk7CgltdWx0aU1hcC5pbnNlcnQoc3RkOjptYWtlX3BhaXIoMSwgMikpOwoJbXVsdGlNYXAuaW5zZXJ0KHN0ZDo6bWFrZV9wYWlyKDIsIDMpKTsKCW11bHRpTWFwLmluc2VydChzdGQ6Om1ha2VfcGFpcigyLCA0KSk7CgltdWx0aU1hcC5pbnNlcnQoc3RkOjptYWtlX3BhaXIoMiwgNSkpOwoJbXVsdGlNYXAuaW5zZXJ0KHN0ZD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