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

原文地址:https://www.cnblogs.com/lit10050528/p/4054114.html