Given an array of const char*
s like that accepted by the nsStaticCaseInsensitiveNameTable::Init
function, you can initialise an std::unordered_map<>
:
std::unordered_map<const char*, int, my_equal_to<const char*>> string_to_index;
for (int i = 0; i < count; ++i)
string_to_index[aNames[i]] = i;
Unfortunately, the default hashing function - std::hash()
- will has const char*
addresses rather than the pointed-to ASCIIZ textual data, so you'll have to write your own or copy one. There's one called "my_equal_to
" edited into the question at C++ unordered_map with char* as key
The only difference here is that the std::unordered_map
's equivalents to "Lookup
" (you could use operator[]
if you knew the string would appear somewhere, otherwise it's best to use find
) are not case insensitive, so you'd need to call a to_lower()
string-processing function on your key if you weren't sure it was already lower case. (Note that per the nsStaticCastInsensitiveNameTable
header's documentation, the strings it's asked to store must be lowercase as a precondition of use). If having to call some manner of to_lower()
function bothers you, you could trivially wrap the unordered_map
in your own class, that called to_lower()
before forwarding to the search functions.