不区分大小写的std ::字符串集 [英] Case insensitive std::set of strings

查看:121
本文介绍了不区分大小写的std ::字符串集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对不区分大小写的插入或在std :: set中搜索字符串?

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

例如 -

std::set<std::string> s;
s.insert("Hello");
s.insert("HELLO"); //not allowed, string already exists.


推荐答案

您需要定义一个自定义比较器:

You need to define a custom comparator:

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;

这篇关于不区分大小写的std ::字符串集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆