热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C++string大小写转换以及transform,tolower,toupper,用法

C++中没有提供string类型的大小写转换,今天写了一下,方法很多#include<iostream>#include<string>#inclu

C++中没有提供string类型的大小写转换,今天写了一下,方法很多

#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    string s = "Hello World";
    cout <::toupper使用定义在全局空间里的toupper
    cout <::tolower使用定义在全局空间里的tolower
    cout < 
 
查阅了资料, toupper和tolower在C++中定义分std和cctype

而定义在std中时原型为charT toupper (charT c, const locale& loc);

而transform函数:

作用是:将某操作应用于指定范围的每个元素。

transform函数有两个重载版本:

transform(first,last,result,op);//first是容器的首迭代器,last为容器的末迭代器,result为存放结果的容器,op为要进行操作的一元函数对象或sturct、class。

transform(first1,last1,first2,result,binary_op);//first1是第一个容器的首迭代器,last1为第一个容器的末迭代器,first2为第二个容器的首迭代器,result为存放结果的容器,binary_op为要进行操作的二元函数对象或sturct、class

如上,std中toupper的原型为一个二元函数,所以使用std::toupper会报错:unresolved overloaded function

但是可以这样使用:

#include 
#include 
#include 
using namespace std;

int main()
{
    string s = "Hello World";
    cout < 
 函数指针解决~ 
 

欢迎拍砖,感谢龙哥的教导。




推荐阅读
author-avatar
孤独小舟9
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有