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

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. SQLAlchemy会话与事务控制:互斥锁和共享锁

    关于sqlalchemy,可以细度这个网址:http://www.codexiu.cn/python/SQLAlchemy%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/73 ...

  2. 18.并发类容器MQ

    package demo7.MQ; public class QueueData { private int id; private String name; private String taskC ...

  3. 客户端实现负载均衡:springCloud Ribbon的使用

    Netfilx发布的负载均衡器,是一个基于http.tcp的客户端负载均衡工具,具有控制http.tcp客户端的行为,为ribbon配置服务提供者的地址后,ribbon就 可以经过springClou ...

  4. ecshop ecmall shopex

    ecshop 是一个B2C商城 适合企业及个人快速构建个性化网上商店.系统是基于PHP语言及MYSQL数据库构架开发的跨平台开源程序.(如沃购网)  山大路是dedecms ecmall(ECMall ...

  5. iOS:quartz2D绘图 (动画)

    quartz2D可以用来绘制自己需要的图形,它们绘制出来的是一个静态的图形,那么如何绘制一个动态的图形呢?动态的图形就是动画,所谓动画,其实就是很多张图片在短时间内不停的切换所产生的一种视觉效果.qu ...

  6. Windows DiskPart工具使用

    启动工具 diskpart 列出磁盘列表 list disk 选择磁盘 select disk 1 转换为GPT分区 convert gpt 列出分区 list partition 清除所有分区 cl ...

  7. Storm常见模式——分布式RPC

    Storm常见模式——分布式RPC 本文翻译自:https://github.com/nathanmarz/storm/wiki/Distributed-RPC,作为学习Storm DRPC的资料,转 ...

  8. ElasticSearch reindex报错:the final mapping would have more than 1 type

    ElasticSearch reindex报错:the final mapping would have more than 1 type 学习了:https://blog.csdn.net/qq_2 ...

  9. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何修改代码字体

    工具-选项   TwinCAT,PLC Environment,Text editor,然后在文本区域中修改字体     更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i. ...

  10. iOS 判断NSString是否包含某个字符串

    主要是使用3个方法 rangeOfString    是否包含 hasPrefix      是否在前缀包含 hasSuffix           是否在末尾包含 如代码: //判断字符是否包含某字 ...