Case insensitive std::set of strings

后端 未结 4 1169
深忆病人
深忆病人 2021-01-01 12:44

How do you have a case insensitive insertion Or search of a string in std::set?

For example-

std::set s;
s.insert(\"Hello\");
s.in         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-01 13:32

    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 s;
    

    You may try stricmp or strcoll if strcasecmp is not available.

提交回复
热议问题