#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <tuple>
using container_type = std::multimap<std::string, int>;
std::multimap<std::string, int> original_table =
{
{"a", 1},
{"c", 2},
{"b", 3},
{"b", 4},
{"a", 5},
{"b", 6}
};
void unique(container_type& container)
{
for (auto iter = begin(container); iter != end(container); ++iter)
{
auto last = container.upper_bound(iter->first);
container.erase(next(iter), last);
}
}
template <typename container_t>
void print_associative(const container_t& c)
{
for (auto& item : c)
std::cout << item.first << ": " << item.second << '\n';
}
int main()
{
container_type table(original_table);
std::cout << "Before:\n";
print_associative(table);
unique(table);
std::cout << "\nAfter:\n";
print_associative(table);
using key_type = container_type::key_type;
using mapped_type = container_type::mapped_type;
std::map<key_type, mapped_type> m(begin(original_table), end(original_table));
std::cout << "\nMap:\n";
print_associative(m);
using value_type = container_type::value_type;
auto value_equal = [](value_type& a, value_type&b) { return a.first == b.first; };
container_type unique_copy_result;
std::unique_copy(begin(original_table), end(original_table),
std::inserter(unique_copy_result, begin(unique_copy_result)),
value_equal);
std::cout << "\nUnique_copy:\n";
print_associative(unique_copy_result);
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aXRlcmF0b3I+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDx0dXBsZT4KCnVzaW5nIGNvbnRhaW5lcl90eXBlID0gc3RkOjptdWx0aW1hcDxzdGQ6OnN0cmluZywgaW50PjsKCnN0ZDo6bXVsdGltYXA8c3RkOjpzdHJpbmcsIGludD4gb3JpZ2luYWxfdGFibGUgPQp7CiAgICB7ImEiLCAxfSwKICAgIHsiYyIsIDJ9LAogICAgeyJiIiwgM30sCiAgICB7ImIiLCA0fSwKICAgIHsiYSIsIDV9LAogICAgeyJiIiwgNn0KfTsKCnZvaWQgdW5pcXVlKGNvbnRhaW5lcl90eXBlJiBjb250YWluZXIpCnsKICAgIGZvciAoYXV0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