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

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. NGUI自适应屏幕分辨率

    unity官方承诺的新ui系统一直没有推出来,我们的UI使用的是原生的OnGUI系统,刚好UI需要改版,索性就想迁到NGUI上面来,于是看了一下NGUI源码,发现NGUI可以大大的降低DrawCall ...

  2. 【特别好】【位运算】maximum-xor-of-two-numbers-in-an-array

    https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/ 利用了异或的”自反性“: a ^ b = c,而a ^ b ...

  3. ms sql 在任何位置 添加列

    摘自: http://bbs.csdn.net/topics/40236129 在任何位置插入列:create proc addcolumn@tablename varchar(30),  --表名@ ...

  4. Spark Streaming资源动态分配和动态控制消费速率

    本篇从二个方面讲解: 高级特性: 1.Spark Streaming资源动态分配 2.Spark Streaming动态控制消费速率 原理剖析,动态控制消费速率其后面存在一套理论,资源动态分配也有一套 ...

  5. STL 源代码剖析 算法 stl_algo.h -- next_permutation

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie next_permutation ----------------------------- ...

  6. Windows录音API学习笔记

    Windows录音API学习笔记 结构体和函数信息  结构体 WAVEINCAPS 该结构描述了一个波形音频输入设备的能力. typedef struct { WORD      wMid; 用于波形 ...

  7. 算法笔记_080:蓝桥杯练习 队列操作(Java)

    目录 1 问题描述 2 解决方案 1 问题描述 问题描述 队列操作题.根据输入的操作命令,操作队列(1)入队.(2)出队并输出.(3)计算队中元素个数并输出. 输入格式 第一行一个数字N. 下面N行, ...

  8. eclipse 编译JAVA 项目导入的WEB项目 无法编译问题

    右击你的项目 选择properties  ---->java Build Path--->Default output folder新建一个classes目录就好了 watermark/2 ...

  9. 中国版Azure支持那些版本号Linux

    不在下述列表中的Linux表示尚未经过正式验证,并不意味着不能使用,客户能够通过自行上传镜像文件的方式使用其它Linux版本号,可是不保证是否遇到一些驱动或者兼容问题. 分发 版本号 上次验证时间 驱 ...

  10. 2013夏,iDempiere来了 - v1.0c Installers (Devina LTS Release) 2013-06-27

    怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ iDempiere来了 - v1.0c Installers (Devina LTS R ...