fork(4) 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 cmp1 = [ordering = { "dog"s, "cat"s, "mouse"s, "elephant"s }](const string& lhs, const string& rhs) { return find(cbegin(ordering), cend(ordering), lhs) < find(cbegin(ordering), cend(ordering), rhs); };
  11. const auto cmp2 = [ordering = { "dog", "cat", "mouse", "elephant" }](const string& lhs, const string& rhs) { return find(cbegin(ordering), cend(ordering), lhs) < find(cbegin(ordering), cend(ordering), rhs); };
  12.  
  13. map<string, int, function<bool(const string&, const string&)>> myMap1(cmp1);
  14.  
  15. myMap1["cat"s] = 1;
  16. myMap1["dog"s] = 2;
  17. myMap1["elephant"s] = 3;
  18. myMap1["mouse"s] = 4;
  19. myMap1["rhino"s] = 5;
  20.  
  21. cout << "Wrong:\n";
  22.  
  23. for (auto& i : myMap1) {
  24. cout << '\t' << i.first << ' ' << i.second << endl;
  25. }
  26.  
  27. map<string, int, function<bool(const string&, const string&)>> myMap2(cmp2);
  28.  
  29. myMap2["cat"s] = 1;
  30. myMap2["dog"s] = 2;
  31. myMap2["elephant"s] = 3;
  32. myMap2["mouse"s] = 4;
  33. myMap2["rhino"s] = 5;
  34.  
  35. cout << "Right:\n";
  36.  
  37. for (auto& i : myMap2) {
  38. cout << '\t' << i.first << ' ' << i.second << endl;
  39. }
  40. }
Success #stdin #stdout 0s 3420KB
stdin
Standard input is empty
stdout
Wrong:
	cat 1
	dog 2
	mouse 4
	elephant 5
Right:
	dog 2
	cat 1
	mouse 4
	elephant 3
	rhino 5