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

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. mongo 实时同步工具 mongosync

    文档地址:https://github.com/Qihoo360/mongosync/wiki/%E4%BD%BF%E7%94%A8%E6%A0%B7%E4%BE%8B #数据全量备份mongodum ...

  2. css实现半颗星评分效果

    效果如下: html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  3. vim文本编辑工具—修改文件内容

    在vim中进行文本替换: 1.替换当前行中的from: :s/from/to/    (其中s是英文单词substitute第一个字母,表示替换的意思) :s/from/to/  ==  :.s/fr ...

  4. iOS:iOS中的几种动画

    本文来自收藏,感谢原创博主. iOS中的动画 摘要 本文主要介绍核iOS中的动画:核心动画Core Animation, UIView动画, Block动画, UIImageView的帧动画. 核心动 ...

  5. 【云计算】Ubuntu14.04 搭建GlusterFS集群

    1.修改 /etc/hosts 所有服务节点执行(如果集群中没有DNS,可忽略此步骤): 10.5.25.37 glusterfs-1-5-25-3710.5.25.38 glusterfs-2-5- ...

  6. IT词汇表

    本人采集到了数十万篇中文技术类博客,进行分词后根据出现的词频手工整理了一份IT词汇表,共计12000个,基本囊括了常见的中英文IT词汇,欢迎各位提出交流意见. 点此 下载

  7. 数据採集器服务——Socket(今天才发现AES加解密代码跟贴的时候不一样,貌似乱码,不知什么情况)

    近期刚做的一个项目.关于 Socket TCP 通信. 需求方提供了一个 ARM 机器,及数据採集器,须要我做一个服务端与数据採集器进行交互. 目的: 数据採集器:定时将读取到的数据发送到服务端. 服 ...

  8. Uber 四年时间增长近 40 倍,背后架构揭秘

    据报道,Uber 仅在过去4年的时间里,业务就激增了 38 倍.Uber 首席系统架构师 Matt Ranney 在一个非常有趣和详细的访谈<可扩展的 Uber 实时市场平台>中告诉我们 ...

  9. 一个简单的int型C++单链表的实现

    IntSLList.h //************************ intSLList.h ************************** // singly-linked list ...

  10. linux内核及其模块的查询,加载,卸载 lsusb等

    http://blog.sina.com.cn/s/blog_53e81e2a0100zkxi.html 1,/sbin/update-modules文件,他是一个linux通用的模块管理脚本程序. ...