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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
// http://ideone.com/Ir7I8e
#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);
}
|