fork download
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <map>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10. map<string, int, function<bool(const string&, const string&)>> myMap([ordering = { "dog", "cat", "mouse", "elephant" }](const auto& lhs, const auto& rhs) { return find(cbegin(ordering), cend(ordering), lhs) < find(cbegin(ordering), cend(ordering), rhs); });
  11.  
  12. myMap["cat"s] = 1;
  13. myMap["dog"s] = 2;
  14. myMap["elephant"s] = 3;
  15. myMap["mouse"s] = 4;
  16. myMap["rhino"s] = 5;
  17.  
  18. for (auto& i : myMap) {
  19. cout << i.first << ' ' << i.second << endl;
  20. }
  21. }
Success #stdin #stdout 0s 3420KB
stdin
Standard input is empty
stdout
dog 2
cat 1
mouse 4
elephant 3
rhino 5