在项目中用到对两个字符串进行忽略大小写的比较,有两个方法实现

1、使用C++提供的忽略大小写比较函数实现

代码实现:

 /*
功能 :忽略大小写进行字符串比较
*/ #ifdef __LINUX__
#include <strings.h>
#endif
#include <iostream>
#include <string>
#include <string.h> using namespace std; int main(int argc, char *argv)
{
string strSrc = "Hello, World";
string strDes = "Hello, world";
#ifdef __LINUX__
if (strcasecmp(strSrc.c_str(), strDes.cStr()) == )
{
cout << strSrc << " 等于 " << strDes << endl;
}
else
{
cout << strSrc << " 不等于 " << strDes << endl;
}
#else
if (stricmp(strSrc.c_str(), strDes.c_str()) == )
{
cout << strSrc << " 等于 " << strDes << endl;
}
else
{
cout << strSrc << " 不等于 " << strDes << endl;
}
#endif
return ;
}

使用到的函数不是C++标准库中的函数,windows和Linux下各有不同的实现,所以使用宏定义进行处理实现跨平台

stricmp是windows下提供的函数

strcasecmp是Linux下提供的函数,使用时需要包含头文件strings.h

2、使用toupper函数或者tolower函数将字符串统一转换为大写或小写然后比较

这种方法不用考虑跨平台的问题,因为使用的是C++标准库中的函数实现的。

代码实现:

 /*
* 文件名 : main.cpp
* 功能 : 将字符串转换为大写,使用transform函数
*
*/
#include <iostream>
#include <algorithm> using namespace std; int main(int argc, char **argv)
{
string strTest = "use test.";
transform(strTest.begin(), strTest.end(), strTest.begin(), toupper); for (size_t i = ; i < strTest.size(); i++)
{
cout << strTest[i];
}
cout << endl; return ;
}

此示例代码仅实现了转换为大写,并未比较,里面使用到了STL中algorithm头文件中的一个算法transform,它的用法参见:http://www.cplusplus.com/reference/algorithm/transform/

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

此处第二种方法的代码在Linux下会出错误,需要修改第14行为:

 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用法:
 template< class charT >
charT toupper( charT ch, const locale& loc );

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

 
所以,造成上面的错误,因此需要显示的指定我们传递的是<ctype>中的函数
 int toupper( int ch );

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

C++忽略字符大小写比较的更多相关文章

  1. lintcode :sort letters by case字符大小写排序

    题目 字符大小写排序 给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序. 您在真实的面试中是否遇到过这个题? Yes 样例 给出"abAcD",一个可能的答案为& ...

  2. apache mod_speling.so 忽略URL大小写(自动纠错)

    apache mod_speling.so 忽略URL大小写(自动纠错) 打开配置文件  httpd.conf 加入 LoadModule speling_module modules/mod_spe ...

  3. vim编辑器的使用技巧——忽略字母大小写

    一忽略字母大小写临时生效 底行模式 底行模式下输入set  ic 注意ic是ignorecase的缩写 命令模式 命令模式进行关键字搜索 二忽略字母大小写永久生效 保存到配置文件里面,默认是没有此配置 ...

  4. 读书笔记——Windows环境下32位汇编语言程序设计(9)ANSII字符大小写转大写

    在罗云彬的<Windows环境下32位汇编语言程序设计>中第321页 ... .const szAllowedChar db '0123456789ABCDEFabcdef',08h .. ...

  5. IDEA项目搭建十一——添加拦截器、忽略URL大小写、启动事件

    程序启动时如果需要添加某些初始化代码可以使用以下事件处理 import org.springframework.context.ApplicationEvent; import org.springf ...

  6. C# 去重处理字符大小写

    本文展示了如何对集合去重并且处理大小写

  7. PHP字符串word末字符大小写互换

    要求 给出一个字符串如 “A journey of, a thousand 'miles' must can't \"begin\" with a single step.” ,通 ...

  8. 位运算处理字符大小写转换 - 关联Leetcode 709. 转成小写字母

    大写变小写.小写变大写 : 字符 ^= 32; 大写变小写.小写变小写 : 字符 |= 32; 小写变大写.大写变大写 : 字符 &= -33; 题目 实现函数 ToLowerCase(),该 ...

  9. mysql西文字符大小写重复键问题的解决方法

    ä和a插入到唯一键时总提示重复 总提示:Duplicate entry 'a' for key 'name' 后来发现我用的COLLATE是utf8_general_ci,改为utf8_bin即可,命 ...

随机推荐

  1. EasyUI datagrid 复选框可以多选但不能全选功能实现

    1.功能需求:  实现多选,但是不能够全选功能 2.js代码 //帮卖列表页面,可以多选但是不能够全选实现 $(".datagrid-header-check").children ...

  2. OpenShift应用镜像构建(1) S2I tomcat 镜像定制

    参考并感谢https://www.jianshu.com/p/fd3e62263046 在对接项目制作应用镜像的过程中,经常发现避免不了的是需要写Dockerfile,(当然另外一种方式是直接run一 ...

  3. C,C++经典问题

    C,C++经典问题   1 编程基础 1.1 基本概念 1.1.1 指针的理解:const char*, char const*, char*const的区别问题几乎是C++面试中每次都会有的题目. ...

  4. LaTeX:Figures, Tables, and Equations 插入图表和公式

    Figures To insert a figure in a LaTeX document, you write lines like this: \begin{figure} \centering ...

  5. InternalError: (pymysql.err.InternalError) (1205, u'Lock wait timeout exceeded; try restarting transaction')

    在mysql innodb中使用事务,如果插入或者更新出错,一定要主动显式地执行rollback,否则可能产生不必要的锁而锁住其他的操作 我们在使用数据库的时候,可以使用contextlib,这样异常 ...

  6. Flask如何使用https?

    1 安装python 的 openssl 的类库 pip install pyOpenSSL 2 在 Flask 的代码中可以直接使用,注意ssl_context的值必须是adhoc from fla ...

  7. [转]SSIS ConnectionManager.ConnectionString Property

    本文转自:http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.runtime.connectionmanager.conne ...

  8. AHB总线RAM Verilog实例

    //*************************************************************************** // Copyright(c)2017, L ...

  9. keepalived 配置需要注意的问题

    keepalived 配置过程中遇到了一些问题,做个记录: 1.selinux的影响:keepalived配置了vrrp_script脚本总是无效      注:脚本返回值0代表成功,1或其他非0值代 ...

  10. C#中使用正则

      using System.Text.RegularExpressions;           private void button1_Click(object sender, EventArg ...