<tfoot id='l84gy'></tfoot>

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

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

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

        不区分大小写的 std::string.find()

        时间:2024-05-12

                • <bdo id='tY8PN'></bdo><ul id='tY8PN'></ul>
                • <tfoot id='tY8PN'></tfoot>

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

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

                  问题描述

                  I am using std::string's find() method to test if a string is a substring of another. Now I need case insensitive version of the same thing. For string comparison I can always turn to stricmp() but there doesn't seem to be a stristr().

                  I have found various answers and most suggest using Boost which is not an option in my case. Additionally, I need to support std::wstring/wchar_t. Any ideas?

                  解决方案

                  You could use std::search with a custom predicate.

                  #include <locale>
                  #include <iostream>
                  #include <algorithm>
                  using namespace std;
                  
                  // templated version of my_equal so it could work with both char and wchar_t
                  template<typename charT>
                  struct my_equal {
                      my_equal( const std::locale& loc ) : loc_(loc) {}
                      bool operator()(charT ch1, charT ch2) {
                          return std::toupper(ch1, loc_) == std::toupper(ch2, loc_);
                      }
                  private:
                      const std::locale& loc_;
                  };
                  
                  // find substring (case insensitive)
                  template<typename T>
                  int ci_find_substr( const T& str1, const T& str2, const std::locale& loc = std::locale() )
                  {
                      typename T::const_iterator it = std::search( str1.begin(), str1.end(), 
                          str2.begin(), str2.end(), my_equal<typename T::value_type>(loc) );
                      if ( it != str1.end() ) return it - str1.begin();
                      else return -1; // not found
                  }
                  
                  int main(int arc, char *argv[]) 
                  {
                      // string test
                      std::string str1 = "FIRST HELLO";
                      std::string str2 = "hello";
                      int f1 = ci_find_substr( str1, str2 );
                  
                      // wstring test
                      std::wstring wstr1 = L"ОПЯТЬ ПРИВЕТ";
                      std::wstring wstr2 = L"привет";
                      int f2 = ci_find_substr( wstr1, wstr2 );
                  
                      return 0;
                  }
                  

                  这篇关于不区分大小写的 std::string.find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:基于堆栈缓冲区的 STL 分配器? 下一篇:std::list::remove 方法是否调用每个已删除元素的析构函数?

                  相关文章

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

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

                  2. <legend id='UFWom'><style id='UFWom'><dir id='UFWom'><q id='UFWom'></q></dir></style></legend>