在C++中实现在map中查找key时不区分大小写

144 篇文章 6 订阅
/*
 * @,@Author: ,: your name
 * @,@Date: ,: 2020-12-09 09:25:03
 * @,@LastEditTime: ,: 2020-12-09 09:40:00
 * @,@LastEditors: ,: Please set LastEditors
 * @,@Description: ,: In User Settings Edit
 * @,@FilePath: ,: /pluginManagerFramework/home/ok/test/f.cpp
 */
#include <iostream>
#include <map>
using namespace std;
/************************************************************************/
/* Comparator for case-insensitive comparison in STL assos. containers  */
/************************************************************************/
struct ci_less : std::binary_function<std::string, std::string, bool>
{
  // case-independent (ci) compare_less binary function
  struct nocase_compare : public std::binary_function<unsigned char, unsigned char, bool>
  {
    bool operator()(const unsigned char &c1, const unsigned char &c2) const
    {
      return tolower(c1) < tolower(c2);
    }
  };
  bool operator()(const std::string &s1, const std::string &s2) const
  {
    return std::lexicographical_compare(s1.begin(), s1.end(), // source range
                                        s2.begin(), s2.end(), // dest range
                                        nocase_compare());    // comparison
  }
};

int main()
{
  std::map<std::string, std::string, ci_less> aMap;
  map<string, string, ci_less>::iterator iter;
  aMap.insert(make_pair("hello", "w"));
  aMap.insert(make_pair("String", "orld"));
  aMap.insert(make_pair("HARRY", "world"));
  aMap.insert(make_pair("hI", "world123"));
  aMap.insert(make_pair("Abc", "world234"));
  aMap.insert(make_pair("EI", "world56"));
  aMap.insert(make_pair("At", "world78"));
  aMap.insert(make_pair("Bring123", "world90"));

  iter = aMap.find("HELLO");
  if (iter != aMap.end())
  {
    cout << "Find, the value is " << iter->second << endl;
  }
  else
  {
    cout << "Do not Find" << endl;
  }

  iter = aMap.find("at");
  if (iter != aMap.end())
  {
    cout << "Find, the value is " << iter->second << endl;
  }
  else
  {
    cout << "Do not Find" << endl;
  }

  iter = aMap.find("BRING123");
  if (iter != aMap.end())
  {
    cout << "find! the value is : " << aMap["BRING123"] << endl;
  }
  else
  {
    cout << "not find!" << endl;
  }

  if (aMap.count("BRING123"))
  {
    cout << "find! the value is : " << aMap["BRING123"] << endl;
  }
  else
  {
    cout << " not Find" << endl;
  }

  if (aMap.count("BRING1"))
  {
    cout << "find! the value is : " << aMap["BRING1"] << endl;
  }
  else
  {
    cout << " not Find" << endl;
  }

  return 0;
}

 普通的map只包含了key和value,在比较的时候是严格区分大小写的。C++的Map还提供了一个自定义比较器的定义方式,如果我们定义一个实现了不区分大小写的比较器放到map的声明里,那后面在查找key时就可以不区分大小写了,这里的查找包括find()和count()俩函数。

如果原来构造的map是普通的map,想转换成带自定义比较器的map,也比较简单:

/*
 * @,@Author: ,: your name
 * @,@Date: ,: 2020-12-09 09:25:03
 * @,@LastEditTime: ,: 2020-12-09 10:15:22
 * @,@LastEditors: ,: Please set LastEditors
 * @,@Description: ,: In User Settings Edit
 * @,@FilePath: ,: /pluginManagerFramework/home/ok/test/f.cpp
 */
#include <iostream>
#include <map>
using namespace std;
/************************************************************************/
/* Comparator for case-insensitive comparison in STL assos. containers  */
/************************************************************************/
struct ci_less : std::binary_function<std::string, std::string, bool>
{
  // case-independent (ci) compare_less binary function
  struct nocase_compare : public std::binary_function<unsigned char, unsigned char, bool>
  {
    bool operator()(const unsigned char &c1, const unsigned char &c2) const
    {
      return tolower(c1) < tolower(c2);
    }
  };
  bool operator()(const std::string &s1, const std::string &s2) const
  {
    return std::lexicographical_compare(s1.begin(), s1.end(), // source range
                                        s2.begin(), s2.end(), // dest range
                                        nocase_compare());    // comparison
  }
};

int main()
{
  std::map<std::string, std::string, ci_less> aMap;
  map<string, string, ci_less>::iterator iter;
  std::map<std::string, std::string> bMap;
  
  bMap.insert(make_pair("hello", "w"));
  bMap.insert(make_pair("String", "orld"));
  bMap.insert(make_pair("HARRY", "world"));
  bMap.insert(make_pair("hI", "world123"));
  bMap.insert(make_pair("Abc", "world234"));
  bMap.insert(make_pair("EI", "world56"));
  bMap.insert(make_pair("At", "world78"));
  bMap.insert(make_pair("Bring123", "world90"));
  
  aMap.insert(bMap.begin(), bMap.end()); 

  iter = aMap.find("HELLO");
  if (iter != aMap.end())
  {
    cout << "Find, the value is " << iter->second << endl;
  }
  else
  {
    cout << "Do not Find" << endl;
  }

  iter = aMap.find("at");
  if (iter != aMap.end())
  {
    cout << "Find, the value is " << iter->second << endl;
  }
  else
  {
    cout << "Do not Find" << endl;
  }

  iter = aMap.find("BRING123");
  if (iter != aMap.end())
  {
    cout << "find! the value is : " << aMap["BRING123"] << endl;
  }
  else
  {
    cout << "not find!" << endl;
  }

  if (aMap.count("BRING123"))
  {
    cout << "find! the value is : " << aMap["BRING123"] << endl;
  }
  else
  {
    cout << " not Find" << endl;
  }

  if (aMap.count("BRING1"))
  {
    cout << "find! the value is : " << aMap["BRING1"] << endl;
  }
  else
  {
    cout << " not Find" << endl;
  }

  return 0;
}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值