C++忽略字符大小寫比較


在項目中用到對兩個字符串進行忽略大小寫的比較,有兩個方法實現

1、使用C++提供的忽略大小寫比較函數實現

代碼實現:

 1 /*
 2     功能 :忽略大小寫進行字符串比較
 3 */
 4 
 5 #ifdef __LINUX__
 6 #include <strings.h>
 7 #endif
 8 #include <iostream>
 9 #include <string>
10 #include <string.h>
11 
12 using namespace std;
13 
14 int main(int argc, char *argv)
15 {
16     string strSrc = "Hello, World";
17     string strDes = "Hello, world";
18 #ifdef __LINUX__
19     if (strcasecmp(strSrc.c_str(), strDes.cStr()) == 0)
20     {
21         cout << strSrc << " 等於 " << strDes << endl;
22     }
23     else
24     {
25         cout << strSrc << " 不等於 " << strDes << endl;
26     }
27 #else
28     if (stricmp(strSrc.c_str(), strDes.c_str()) == 0)
29     {
30         cout << strSrc << " 等於 " << strDes << endl;
31     }
32     else
33     {
34         cout << strSrc << " 不等於 " << strDes << endl;
35     }
36 #endif
37     return 0;
38 }

使用到的函數不是C++標准庫中的函數,windows和Linux下各有不同的實現,所以使用宏定義進行處理實現跨平台

stricmp是windows下提供的函數

strcasecmp是Linux下提供的函數,使用時需要包含頭文件strings.h

2、使用toupper函數或者tolower函數將字符串統一轉換為大寫或小寫然后比較

這種方法不用考慮跨平台的問題,因為使用的是C++標准庫中的函數實現的。

代碼實現:

 1 /*
 2 * 文件名    :    main.cpp
 3 * 功能        : 將字符串轉換為大寫,使用transform函數
 4 *
 5 */
 6 #include <iostream>
 7 #include <algorithm>
 8 
 9 using namespace std;
10 
11 int main(int argc, char **argv)
12 {
13     string strTest = "use test.";
14     transform(strTest.begin(), strTest.end(), strTest.begin(), toupper);
15     
16     for (size_t i = 0; i < strTest.size(); i++)
17     {
18         cout << strTest[i];
19     }
20     cout << endl;
21 
22     return 0;
23 }

此示例代碼僅實現了轉換為大寫,並未比較,里面使用到了STL中algorithm頭文件中的一個算法transform,它的用法參見:http://www.cplusplus.com/reference/algorithm/transform/

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

此處第二種方法的代碼在Linux下會出錯誤,需要修改第14行為:

1 transform(strTest.begin(), strTest.end(), strTest.begin(), ::toupper);

參考:

http://blog.csdn.net/delphiwcdj/article/details/6890668
http://forums.codeguru.com/showthread.php?489969-no-matching-function-transform
---------------------------------------------------------------------------------------------------------------------------------------------------
transform定義:http://www.cplusplus.com/reference/algorithm/transform/
unary operation(1)
template <class InputIterator, class OutputIterator, class UnaryOperation>
  OutputIterator transform (InputIterator first1, InputIterator last1,
                            OutputIterator result, UnaryOperation op);
binary operation(2)
template <class InputIterator1, class InputIterator2,
          class OutputIterator, class BinaryOperation>
  OutputIterator transform (InputIterator1 first1, InputIterator1 last1,
                            InputIterator2 first2, OutputIterator result,
                            BinaryOperation binary_op);
顯然,我們使用的是第一個,也就是需要傳遞一個一元運算符,而
std::toupper用法:
1 template< class charT >
2 charT toupper( charT ch, const locale& loc );

參考:http://en.cppreference.com/w/cpp/locale/toupper

 
所以,造成上面的錯誤,因此需要顯示的指定我們傳遞的是<ctype>中的函數
1 int toupper( int ch );

參考:http://en.cppreference.com/w/cpp/string/byte/toupper


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM