1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <map>
#include <iostream>
bool display(const std::multimap<int, int>& mm) {
for (const auto& [k, v] : mm)
std::cout << '(' << k << ',' << v << ") ";
return !!(std::cout << "\n\n");
}
int main() {
std::multimap<int, int> mm { {1, 1}, {1, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3} };
for (bool again { display(mm)}; again; ) {
int key {};
std::cout << "Enter key to erase: ";
std::cin >> key;
const auto bounds { mm.equal_range(key) };
if (bounds.first == mm.end())
std::cout << "Key not found\n";
else
if (std::distance(bounds.first, bounds.second) == 1)
mm.erase(bounds.first);
else {
std::cout << "There are multiple keys\n";
size_t elem {}, val {};
for (auto itr(bounds.first); itr != bounds.second; ++itr, ++elem)
std::cout << elem + 1 << ". " << itr->second << '\n';
std::cout << "Which one to erase: ";
std::cin >> val;
if (val == 0 || val > elem)
std::cout << "Invalid value\n";
else
mm.erase(std::next(bounds.first, val - 1));
}
display(mm);
char res {};
std::cout << "Erase another (y/n) :";
std::cin >> res;
again = res == 'Y' || res == 'y';
}
}
|