How do you have a case insensitive insertion Or search of a string in std::set?
For example-
std::set<std::string> s; s.insert("Hello"); s.insert("HELLO"); //not allowed, string already exists.
String comparison is case sensitive by default. Just use operator== for std::string .
Comparing strings in a case insensitive manner means to compare them without taking care of the uppercase and lowercase letters. To perform this operation the most preferred method is to use either toUpperCase() or toLowerCase() function.
You need to define a custom comparator:
struct InsensitiveCompare { bool operator() (const std::string& a, const std::string& b) const { return strcasecmp(a.c_str(), b.c_str()) < 0; } }; std::set<std::string, InsensitiveCompare> s;
You may try stricmp
or strcoll
if strcasecmp
is not available.
std::set offers the possibility of providing your own comparer (as do most std containers). You can then perform any type of comparison you like. Full example is available here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With