<bdo id='BrVNj'></bdo><ul id='BrVNj'></ul>
<i id='BrVNj'><tr id='BrVNj'><dt id='BrVNj'><q id='BrVNj'><span id='BrVNj'><b id='BrVNj'><form id='BrVNj'><ins id='BrVNj'></ins><ul id='BrVNj'></ul><sub id='BrVNj'></sub></form><legend id='BrVNj'></legend><bdo id='BrVNj'><pre id='BrVNj'><center id='BrVNj'></center></pre></bdo></b><th id='BrVNj'></th></span></q></dt></tr></i><div id='BrVNj'><tfoot id='BrVNj'></tfoot><dl id='BrVNj'><fieldset id='BrVNj'></fieldset></dl></div>
<tfoot id='BrVNj'></tfoot>

        <legend id='BrVNj'><style id='BrVNj'><dir id='BrVNj'><q id='BrVNj'></q></dir></style></legend>

      1. <small id='BrVNj'></small><noframes id='BrVNj'>

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

        时间:2024-05-11

          <small id='xGEB1'></small><noframes id='xGEB1'>

          <legend id='xGEB1'><style id='xGEB1'><dir id='xGEB1'><q id='xGEB1'></q></dir></style></legend>
            <bdo id='xGEB1'></bdo><ul id='xGEB1'></ul>
              <tbody id='xGEB1'></tbody>

                1. <i id='xGEB1'><tr id='xGEB1'><dt id='xGEB1'><q id='xGEB1'><span id='xGEB1'><b id='xGEB1'><form id='xGEB1'><ins id='xGEB1'></ins><ul id='xGEB1'></ul><sub id='xGEB1'></sub></form><legend id='xGEB1'></legend><bdo id='xGEB1'><pre id='xGEB1'><center id='xGEB1'></center></pre></bdo></b><th id='xGEB1'></th></span></q></dt></tr></i><div id='xGEB1'><tfoot id='xGEB1'></tfoot><dl id='xGEB1'><fieldset id='xGEB1'></fieldset></dl></div>
                2. <tfoot id='xGEB1'></tfoot>
                  本文介绍了如何使 map::find 操作不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  map<string, vector<string> > directory;
                  

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

                  and want the below search to ignore case:

                  directory.find(search_string);
                  

                  推荐答案

                  默认情况下没有.您必须提供一个自定义比较器作为第三个参数.以下代码段将帮助您...

                  It does not by default. You will have to provide a custom comparator as a third argument. Following snippet will help you...

                    /************************************************************************/
                    /* 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, ci_less >myMap;

                  注意:std::lexicographical_compare 有一些细节.如果您考虑语言环境,字符串比较并不总是那么简单.如果有兴趣,请参阅 clc++ 上的 this 线程.

                  NOTE: std::lexicographical_compare has some nitty-gritty details. String comparison isn't always straightforward if you consider locales. See this thread on c.l.c++ if interested.

                  更新:在 C++11 中 std::binary_function 已被弃用,因为类型是自动推导出来的,所以没有必要.

                  UPDATE: With C++11 std::binary_function is deprecated and is unnecessary as the types are deduced automatically.

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

                  这篇关于如何使 map::find 操作不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何对 STL 向量进行排序? 下一篇:为什么 std::list 没有运算符 []?

                  相关文章

                3. <tfoot id='JYng1'></tfoot>

                  1. <i id='JYng1'><tr id='JYng1'><dt id='JYng1'><q id='JYng1'><span id='JYng1'><b id='JYng1'><form id='JYng1'><ins id='JYng1'></ins><ul id='JYng1'></ul><sub id='JYng1'></sub></form><legend id='JYng1'></legend><bdo id='JYng1'><pre id='JYng1'><center id='JYng1'></center></pre></bdo></b><th id='JYng1'></th></span></q></dt></tr></i><div id='JYng1'><tfoot id='JYng1'></tfoot><dl id='JYng1'><fieldset id='JYng1'></fieldset></dl></div>

                  2. <small id='JYng1'></small><noframes id='JYng1'>

                    • <bdo id='JYng1'></bdo><ul id='JYng1'></ul>

                    <legend id='JYng1'><style id='JYng1'><dir id='JYng1'><q id='JYng1'></q></dir></style></legend>