#include <algorithm> #include <cctype> #include <functional> #include <iostream> #include <map> #include <iostream> template <typename map_type> void verify_presence_of_key(const map_type& map, const typename map_type::key_type& checkme) { auto it = map.find(checkme); std::cout << '"' << checkme << "\": "; if (it == map.end()) std::cout << "Not found!\n"; else std::cout << "Found! -- Key = \"" << it->first << "\", Value = \"" << it->second << "\".\n"; } int main() { struct caseless_less { bool operator()(const std::string& a, const std::string& b) const { auto a_ = a.begin(); auto b_ = b.begin(); while (a_ != a.end() && b_ != b.end()) { auto lower_a = std::tolower(*a_++); auto lower_b = std::tolower(*b_++); if (lower_a < lower_b) return true; if (lower_a > lower_b) return false; } return false; } }; std::map<std::string, unsigned, caseless_less> my_map; { std::string s = "abcd"; unsigned val = 1; do { my_map[s] = val++; } while (std::next_permutation(s.begin(), s.end())); // remove a few to make things more interesting. my_map.erase("cabd"); my_map.erase("adcb"); } { std::string s = "AbCd"; std::sort(s.begin(), s.end()); do { verify_presence_of_key(my_map, s); } while (std::next_permutation(s.begin(), s.end())); } }
Standard input is empty
"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".