fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <map>
  5. #include <algorithm>
  6.  
  7. const std::vector<std::string> ordering {"dog", "cat", "mouse", "elephant"};
  8.  
  9. struct cmp
  10. {
  11. bool operator()(const std::string& lhs, const std::string& rhs)
  12. {
  13. return (std::find(ordering.begin(), ordering.end(), lhs) <
  14. std::find(ordering.begin(), ordering.end(), rhs));
  15. }
  16. };
  17.  
  18. using MyMap = std::map<std::string, int, cmp>;
  19.  
  20. int main()
  21. {
  22. MyMap a;
  23. a["elephant"] = 10;
  24. a["mouse"]= 20;
  25. for ( auto& item : a )
  26. {
  27. std::cout << item.first << " " << item.second << std::endl;
  28. }
  29. }
  30.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
mouse 20
elephant 10