fork download
  1. #include <algorithm>
  2. #include <cctype>
  3. #include <functional>
  4. #include <iostream>
  5. #include <map>
  6. #include <iostream>
  7.  
  8. template <typename map_type>
  9. void verify_presence_of_key(const map_type& map, const typename map_type::key_type& checkme)
  10. {
  11. auto it = map.find(checkme);
  12.  
  13. std::cout << '"' << checkme << "\": ";
  14.  
  15. if (it == map.end())
  16. std::cout << "Not found!\n";
  17. else
  18. std::cout << "Found! -- Key = \"" << it->first << "\", Value = \"" << it->second << "\".\n";
  19. }
  20.  
  21. int main()
  22. {
  23. struct caseless_less
  24. {
  25. bool operator()(const std::string& a, const std::string& b) const
  26. {
  27. auto a_ = a.begin();
  28. auto b_ = b.begin();
  29.  
  30. while (a_ != a.end() && b_ != b.end())
  31. {
  32. auto lower_a = std::tolower(*a_++);
  33. auto lower_b = std::tolower(*b_++);
  34.  
  35. if (lower_a < lower_b)
  36. return true;
  37.  
  38. if (lower_a > lower_b)
  39. return false;
  40. }
  41.  
  42. return false;
  43. }
  44. };
  45.  
  46. std::map<std::string, unsigned, caseless_less> my_map;
  47.  
  48. {
  49. std::string s = "abcd";
  50. unsigned val = 1;
  51. do {
  52. my_map[s] = val++;
  53. } while (std::next_permutation(s.begin(), s.end()));
  54.  
  55. // remove a few to make things more interesting.
  56. my_map.erase("cabd");
  57. my_map.erase("adcb");
  58. }
  59.  
  60. {
  61. std::string s = "AbCd";
  62. std::sort(s.begin(), s.end());
  63. do {
  64. verify_presence_of_key(my_map, s);
  65. } while (std::next_permutation(s.begin(), s.end()));
  66. }
  67.  
  68. }
Success #stdin #stdout 0s 3280KB
stdin
Standard input is empty
stdout
"ACbd": Found! -- Key = "acbd", Value = "3".
"ACdb": Found! -- Key = "acdb", Value = "4".
"AbCd": Found! -- Key = "abcd", Value = "1".
"AbdC": Found! -- Key = "abdc", Value = "2".
"AdCb": Not found!
"AdbC": Found! -- Key = "adbc", Value = "5".
"CAbd": Not found!
"CAdb": Found! -- Key = "cadb", Value = "14".
"CbAd": Found! -- Key = "cbad", Value = "15".
"CbdA": Found! -- Key = "cbda", Value = "16".
"CdAb": Found! -- Key = "cdab", Value = "17".
"CdbA": Found! -- Key = "cdba", Value = "18".
"bACd": Found! -- Key = "bacd", Value = "7".
"bAdC": Found! -- Key = "badc", Value = "8".
"bCAd": Found! -- Key = "bcad", Value = "9".
"bCdA": Found! -- Key = "bcda", Value = "10".
"bdAC": Found! -- Key = "bdac", Value = "11".
"bdCA": Found! -- Key = "bdca", Value = "12".
"dACb": Found! -- Key = "dacb", Value = "20".
"dAbC": Found! -- Key = "dabc", Value = "19".
"dCAb": Found! -- Key = "dcab", Value = "23".
"dCbA": Found! -- Key = "dcba", Value = "24".
"dbAC": Found! -- Key = "dbac", Value = "21".
"dbCA": Found! -- Key = "dbca", Value = "22".