如何使map :: find操作不区分大小写?

问题描述 投票:45回答:11

map::find方法是否支持不区分大小写的搜索?我有一张地图如下:

map<string, vector<string> > directory;

并希望以下搜索忽略大小写:

directory.find(search_string);
c++ string dictionary stl case-insensitive
11个回答
65
投票

它不是默认的。您必须提供自定义比较器作为第三个参数。以下片段将帮助您......

  /************************************************************************/
  /* 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
    }
  };

std::map< std::string, std::vector<std::string>, ci_less > myMap;一样使用它

注意:std :: lexicographical_compare有一些细节。如果考虑区域设置,字符串比较并不总是直截了当。如果感兴趣,请参阅c.l.c ++上的this thread。

更新:使用C ++ 11时,不推荐使用std::binary_function,因为类型是自动推断的。

  struct ci_less
  {
    // case-independent (ci) compare_less binary function
    struct nocase_compare
    {
      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
    }
  };

0
投票

实现std :: less函数并通过将两者都更改为相同的情况进行比较。


0
投票

我想在不使用Boost或模板的情况下提供简短的解决方案。从C++11开始,您还可以将lambda expression作为自定义比较器提供给您的地图。对于POSIX兼容系统,解决方案可能如下所示:

auto comp = [](const std::string& s1, const std::string& s2) {
    return strcasecmp(s1.c_str(), s2.c_str()) < 0;
};
std::map<std::string, std::vector<std::string>, decltype(comp)> directory(comp);

Code on Ideone

对于Window,strcasecmp()不存在,但您可以使用_stricmp()代替:

auto comp = [](const std::string& s1, const std::string& s2) {
    return _stricmp(s1.c_str(), s2.c_str()) < 0;
};
std::map<std::string, std::vector<std::string>, decltype(comp)> directory(comp);

注意:根据您的系统以及是否必须支持Unicode,您可能需要以不同的方式比较字符串。 This Q&A开了个好头。


23
投票

以下是一些其他替代方案,包括性能明显更快的方案。

#include    <map>
#include    <string>
#include    <cstring>
#include    <iostream>
#include    <boost/algorithm/string.hpp>

using std::string;
using std::map;
using std::cout;
using std::endl;

using namespace boost::algorithm;

// recommended in Meyers, Effective STL when internationalization and embedded
// NULLs aren't an issue.  Much faster than the STL or Boost lex versions.
struct ciLessLibC : public std::binary_function<string, string, bool> {
    bool operator()(const string &lhs, const string &rhs) const {
        return strcasecmp(lhs.c_str(), rhs.c_str()) < 0 ;
    }
};

// Modification of Manuel's answer
struct ciLessBoost : std::binary_function<std::string, std::string, bool>
{
    bool operator() (const std::string & s1, const std::string & s2) const {
        return lexicographical_compare(s1, s2, is_iless());
    }
};

typedef map< string, int, ciLessLibC> mapLibc_t;
typedef map< string, int, ciLessBoost> mapBoost_t;

int main(void) {
    mapBoost_t cisMap; // change to test other comparitor 

    cisMap["foo"] = 1;
    cisMap["FOO"] = 2;

    cisMap["bar"] = 3;
    cisMap["BAR"] = 4;

    cisMap["baz"] = 5;
    cisMap["BAZ"] = 6;

    cout << "foo == " << cisMap["foo"] << endl;
    cout << "bar == " << cisMap["bar"] << endl;
    cout << "baz == " << cisMap["baz"] << endl;

    return 0;
}

6
投票

您可以使用三个参数来实例化std::map:键的类型,值的类型和比较函数 - 您喜欢的严格弱排序(本质上,函数或函数表现为operator<在传递性和反自反性方面)。只需定义第三个参数来做“不区分大小写”(例如,通过它所比较的​​小写字符串上的<),你就会得到你想要的“不区分大小写的地图”!


5
投票

我使用以下内容:

bool str_iless(std::string const & a, 
               std::string const & b)
{
    return boost::algorithm::lexicographical_compare(a, b,  
                                                     boost::is_iless());
}
std::map<std::string, std::string, 
         boost::function<bool(std::string const &, 
                              std::string const &)> 
         > case_insensitive_map(&str_iless);

4
投票

如果您不想触摸地图类型(以保持其原始的简单性和效率),但不介意使用较慢的不区分大小写的查找功能(O(N)):

string to_lower(string s) {
    transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower );
    return s;
}

typedef map<string, int> map_type;

struct key_lcase_equal {
    string lcs;
    key_lcase_equal(const string& s) : lcs(to_lower(s)) {}
    bool operator()(const map_type::value_type& p) const {
        return to_lower(p.first) == lcs;
    }
};

map_type::iterator find_ignore_case(map_type& m, const string& s) {
    return find_if(m.begin(), m.end(), key_lcase_equal(s));
}

PS:也许这是Roger Pate的想法,但不确定,因为有些细节有点过时(std :: search ?,直接字符串比较器?)


3
投票

不,你不能使用find这样做,因为在那种情况下会有多个匹配。例如,虽然插入让你做了像map["A"] = 1map["a"] = 2这样的事情,现在如果你想要一个不区分大小写的map.find("a"),那么预期的返回值是多少?解决这个问题的最简单方法是在一个案例(大写或小写)中将字符串插入映射,然后在执行查找时使用相同的大小写。


1
投票

地图模板的Compare元素默认为二进制比较类“less”。看看实现:

http://www.cplusplus.com/reference/std/functional/less/

您可以创建自己的类,该类派生自binary_function(父类为less)并进行相同的比较,不区分大小写。


1
投票

测试:

template<typename T>
struct ci_less:std::binary_function<T,T,bool>
  { bool operator() (const T& s1,const T& s2) const { return boost::ilexicographical_compare(s1,s2); }};

...

map<string,int,ci_less<string>> x=boost::assign::map_list_of
        ("One",1)
        ("Two",2)
        ("Three",3);

cout << x["one"] << x["TWO"] <<x["thrEE"] << endl;

//Output: 123

1
投票

对于C ++ 11及更高版本:

#include <strings.h>
#include <map>
#include <string>

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 <typename T>
using CaseInsensitiveMap = std::map<std::string, T, detail::CaseInsensitiveComparator>;



int main(int argc, char* argv[])
{
    CaseInsensitiveMap<int> m;

    m["one"] = 1;
    std::cout << m.at("ONE") << "\n";

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.