I want to implement an unordered_map<string, string> that ignores case in the keys. My code looks like:
std::unordered_map<std::string, std::string> noCaseMap()
{
struct hasher {
std::size_t operator()(const std::string& key) const {
return std::hash<std::string>{}(toLower(key));
}
};
std::unordered_map<std::string, std::string, hasher> ret;
return ret;
}
but XCode flags the return statement with this error:
foo.cpp:181:20 No viable conversion from returned value of type 'unordered_map<[2 * ...], hasher>' to function return type 'unordered_map<[2 * ...], (default) std::hash<std::string>>'
I tried casting ret to <std::unordered_map<std::string, std::string>>, but XCode wasn't having it.
I tried making my hasher a subclass of std::hash<std::string> but that made no difference.
Edit: this is a slight oversimplification of the problem; I know I also have to implement a case-insensitive equal_to() functor as well.
hasherat namespace scope and adjust the return type? Also note thatstd::unordered_map<std::string, std::string, hasher>probably doesn't do what you want: this just uses a worse hash function, but it doesn't change anything about key equality. Inserting values for"a"and"A"into your map will still result in 2 entries, but those 2 entries will always result in a hash collision.std::unordered_map<Foo, std::string>whereFoocan be constructed from a string and stores a lower case string. When the user then looks upthe_map["AsDf"]then their string will be converted to lower case