The following question was worded such that I thought it would get me what I need:
Case insensitive std::set of strings
Note that they mention case insensitive search as well as insertion. However, when I implement the answer using a custom comparator, I get case insensitive insertion but not search. Maybe I'm misunderstanding what is referred to as "search?" Here's a minimal example of what I'm seeing:
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
struct InsensitiveCompare {
bool
operator()(std::string_view a, std::string_view b) const
{
return std::equal(
a.begin(),
a.end(),
b.begin(),
b.end(),
[](char a, char b) {
return tolower(a) == tolower(b);
}
);
}
};
std::unordered_set<std::string, std::hash<std::string>, InsensitiveCompare> s;
int
main(int argc, char* argv[])
{
s.insert("one");
s.insert("oNe");
s.insert("tWo");
cout << "size: " << s.size() << endl;
auto found = s.find("TWO");
cout << "found(TWO): " << (found != s.end()) << endl;
found = s.find("tWo");
cout << "found(tWo): " << (found != s.end()) << endl;
return 0;
}
This produces the following output:
g++ -g -Wall -Werror -std=c++17 test.cc -o test
./test
size: 2
found(TWO): 0
found(tWo): 1
Notice that find is behaving in a case sensitive manner: tWo
matches but TWO
doesn't. I desire both to match. What are my options for making find case insensitive.