Другое решение состоит в том, чтобы скопировать unordered_map
в multimap
со значениями, являющимися ключами, а затем распечатать его:
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
#include <atomic>
#include <algorithm>
#include <iterator>
int main()
{
std::unordered_map<std::string, std::atomic<unsigned int>> m;
for (auto p : std::initializer_list<std::pair<std::string, unsigned int>>{{ "a", 32},{ "b" , 22 },{ "c" , 32 },{ "d" , 22 },{ "e" , 55 } })
m.emplace(p);
std::multimap<unsigned int, std::string> printmap;
std::transform(m.begin(), m.end(), std::inserter(printmap, printmap.end()),
[](auto const &p) { return std::make_pair(p.second.load(), p.first); });
for (auto const &p : printmap)
std::cout << p.first << " - " << p.second << std::endl;
return 0;
}
Демо: https://ideone.com/MgtMY8
22 - d
22 - b
32 - c
32 - a
55 - e