How can I make the map::find operation case insensitive?

前端 未结 11 1848
野的像风
野的像风 2020-12-01 00:23

Does the map::find method support case insensitive search? I have a map as follows:

map > directory;
<         


        
11条回答
  •  醉梦人生
    2020-12-01 00:52

    For C++11 and beyond:

    #include 
    #include 
    #include 
    
    namespace detail
    {
    
    struct CaseInsensitiveComparator
    {
        bool operator()(const std::string& a, const std::string& b) const noexcept
        {
            return ::strcasecmp(a.c_str(), b.c_str()) < 0;
        }
    };
    
    }   // namespace detail
    
    
    template 
    using CaseInsensitiveMap = std::map;
    
    
    
    int main(int argc, char* argv[])
    {
        CaseInsensitiveMap m;
    
        m["one"] = 1;
        std::cout << m.at("ONE") << "\n";
    
        return 0;
    }
    

提交回复
热议问题