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

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

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

        <bdo id='iAj7K'></bdo><ul id='iAj7K'></ul>
    1. <tfoot id='iAj7K'></tfoot>

      1. std::map 键的部分匹配

        时间:2023-09-18
      2. <tfoot id='rJKtT'></tfoot>
            <bdo id='rJKtT'></bdo><ul id='rJKtT'></ul>

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

                    <tbody id='rJKtT'></tbody>

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

                • <legend id='rJKtT'><style id='rJKtT'><dir id='rJKtT'><q id='rJKtT'></q></dir></style></legend>
                • 本文介绍了std::map 键的部分匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 std::map 并且我想使用子字符串搜索一个键.例如,我有以下代码:

                  I have an std::map and I want to search for a key using a substring. For example, I have the following code:

                  #include <iostream>
                  #include <map>
                  #include <string>
                  using namespace std;
                  
                  typedef std::map<std::string, std::string> TStrStrMap;
                  typedef std::pair<std::string, std::string> TStrStrPair;
                  
                  int main(int argc, char *argv[])
                  {
                      TStrStrMap tMap;
                  
                      tMap.insert(TStrStrPair("John", "AA"));
                      tMap.insert(TStrStrPair("Mary", "BBB"));
                      tMap.insert(TStrStrPair("Mother", "A"));
                      tMap.insert(TStrStrPair("Marlon", "C"));
                  
                      return 0;
                  }
                  

                  现在,如果Marla"存储在地图中,我想搜索包含子字符串Marl"而不是Marlon"的位置.我想找到以Marl"开头的东西.我最多需要找到一个位置.这可能吗?如果是这样,如何?

                  Now, I want to search for the position that holds the substring "Marl" and not "Marlon", if "Marla" is stored in the map. I want to find something that starts with "Marl". I need to find at most one position. Is this possible? If so, how?

                  我不想使用任何 Boost 库!

                  I don't want to use any Boost libraries!

                  推荐答案

                  你不能高效搜索子字符串,但你可以搜索前缀:

                  You can't efficiently search for substring, but you can for prefix:

                  #include <iostream>
                  #include <map>
                  #include <string>
                  #include <algorithm>
                  using namespace std;
                  
                  typedef map<string, string> TStrStrMap;
                  typedef pair<string, string> TStrStrPair;
                  
                  TStrStrMap::const_iterator FindPrefix(const TStrStrMap& map, const string& search_for) {
                      TStrStrMap::const_iterator i = map.lower_bound(search_for);
                      if (i != map.end()) {
                          const string& key = i->first;
                          if (key.compare(0, search_for.size(), search_for) == 0) // Really a prefix?
                              return i;
                      }
                      return map.end();
                  }
                  
                  void Test(const TStrStrMap& map, const string& search_for) {
                      cout << search_for;
                      auto i = FindPrefix(map, search_for);
                      if (i != map.end())
                          cout << '	' << i->first << ", " << i->second;
                      cout << endl;
                  }
                  
                  int main(int argc, char *argv[])
                  {
                      TStrStrMap tMap;
                  
                      tMap.insert(TStrStrPair("John", "AA"));
                      tMap.insert(TStrStrPair("Mary", "BBB"));
                      tMap.insert(TStrStrPair("Mother", "A"));
                      tMap.insert(TStrStrPair("Marlon", "C"));
                  
                      Test(tMap, "Marl");
                      Test(tMap, "Mo");
                      Test(tMap, "ther");
                      Test(tMap, "Mad");
                      Test(tMap, "Mom");
                      Test(tMap, "Perr");
                      Test(tMap, "Jo");
                  
                      return 0;
                  }
                  

                  打印:

                  Marl    Marlon, C
                  Mo      Mother, A
                  ther
                  Mad
                  Mom
                  Perr
                  Jo      John, AA
                  

                  这篇关于std::map 键的部分匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:什么是最好的自动完成/建议算法,数据结构 [C 下一篇:哪些(如果有)C++ 编译器进行尾递归优化?

                  相关文章

                  最新文章

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

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

                  1. <legend id='iIWkI'><style id='iIWkI'><dir id='iIWkI'><q id='iIWkI'></q></dir></style></legend>

                      <bdo id='iIWkI'></bdo><ul id='iIWkI'></ul>

                    <tfoot id='iIWkI'></tfoot>