#include
#include
#include
#include
using namespace std;
int main()
{
string s = “Hello World”;
cout << s << endl;
transform(s.begin(),s.end(),s.begin(),::toupper);//小写转大写
cout << s << endl;
transform(s.begin(),s.end(),s.begin(),::tolower);大写转小写
cout << s << endl;
return 0;
}
查阅了资料,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 << s << endl;
transform(s.begin(),s.end(),s.begin(),(int ()(int))toupper);
cout << s << endl;
transform(s.begin(),s.end(),s.begin(),(int ()(int))tolower);
cout << s << endl;
return 0;
}
函数指针解决
转载:https://blog.csdn.net/qq_31186409/article/details/50545682