fork(2) download
  1. #include <map>
  2. #include <set>
  3. #include <string>
  4. #include <iostream>
  5. #include <iomanip>
  6.  
  7. int main() {
  8. std::map<std::string, int> m; // Your original map.
  9. m["realistically"] = 1;
  10. m["really"] = 8;
  11. m["reason"] = 4;
  12. m["reasonable"] = 3;
  13. m["reasonably"] = 1;
  14. m["reassemble"] = 1;
  15. m["reassembled"] = 1;
  16. m["recognize"] = 2;
  17. m["record"] = 92;
  18. m["records"] = 48;
  19. m["recs"] = 7;
  20.  
  21. std::set<std::pair<int, std::string>> s; // The new (temporary) container.
  22.  
  23. for (auto const &kv : m)
  24. s.emplace(kv.second, kv.first); // Flip the pairs.
  25.  
  26. for (auto const &vk : s)
  27. std::cout << std::setw(3) << vk.first << std::setw(15) << vk.second << std::endl;
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
  1  realistically
  1     reasonably
  1     reassemble
  1    reassembled
  2      recognize
  3     reasonable
  4         reason
  7           recs
  8         really
 48        records
 92         record