你如何在std :: set中进行不区分大小写的插入或搜索字符串?
例如-
std::set<std::string> s;
s.insert("Hello");
s.insert("HELLO"); //not allowed, string already exists.
Run Code Online (Sandbox Code Playgroud)
ybu*_*ill 35
您需要定义自定义比较器:
struct InsensitiveCompare {
bool operator() (const std::string& a, const std::string& b) const {
return stricmp(a.c_str(), b.c_str()) < 0;
}
};
std::set<std::string, InsensitiveCompare> s;
Run Code Online (Sandbox Code Playgroud)