fork(1) download
  1. #include <string>
  2. #include <iostream>
  3. #include <vector>
  4. #include <atomic>
  5. #include <algorithm>
  6. #include <unordered_map>
  7. using namespace std;
  8.  
  9. int main() {
  10. std::unordered_map<std::string, std::atomic<unsigned int>> m;
  11. m["quick"] = 0;
  12. m["brown"] = 1;
  13. m["fox"] = 2;
  14. m["jumps"] = 3;
  15. m["over"] = 4;
  16. m["the"] = 5;
  17. m["lazy"] = 6;
  18. m["dog"] = 7;
  19. std::vector<pair<unsigned int,std::string>> copy;
  20. std::transform(
  21. m.begin()
  22. , m.end()
  23. , back_inserter(copy)
  24. , [](const pair<const std::string, std::atomic<unsigned int>>& p) {
  25. return make_pair(p.second.load(), p.first);
  26. });
  27. for (auto it : copy)
  28. std::cout << " " << it.first << ":" << it.second << std::endl;
  29. std::cout << "==========" << std::endl;
  30. std::sort(copy.begin(), copy.end());
  31. for (auto it : copy)
  32. std::cout << " " << it.first << ":" << it.second << std::endl;
  33. return 0;
  34. }
Success #stdin #stdout 0s 4372KB
stdin
Standard input is empty
stdout
 7:dog
 6:lazy
 5:the
 3:jumps
 4:over
 0:quick
 1:brown
 2:fox
==========
 0:quick
 1:brown
 2:fox
 3:jumps
 4:over
 5:the
 6:lazy
 7:dog