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. const auto cmp = [ordering = { "dog", "cat", "mouse", "elephant" }](const string& lhs, const string& rhs) { return find(cbegin(ordering), cend(ordering), lhs) < find(cbegin(ordering), cend(ordering), rhs); };
  11.  
  12. map<string, int, function<bool(const string&, const string&)>> myMap(cmp);
  13.  
  14. myMap["cat"s] = 1;
  15. myMap["dog"s] = 2;
  16. myMap["elephant"s] = 3;
  17. myMap["mouse"s] = 4;
  18. myMap["rhino"s] = 5;
  19.  
  20. for (auto& i : myMap) {
  21. cout << i.first << ' ' << i.second << endl;
  22. }
  23. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
dog 2
cat 1
mouse 4
elephant 3
rhino 5