概念
transform,一个区间元素交换函数。
1.string小写变大写
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
//仿函数
class myToUpper
{
public:
char operator()(char ch)
{
if (ch >= 'a' && ch <= 'z')
ch = ch - 32;
return ch;
}
};
int main()
{
string str1 = "abCd";
string str2;
str2.resize(str1.size()); //不可缺
myToUpper fun1;
transform(str1.begin(), str1.end(), str1.begin(), fun1);
//把fun1返回的值给第三个参数的对象
transform(str1.begin(), str1.end(), str2.begin(), fun1);
//把str1中字母大写放入str2中,fun1也可以改为系统提供的toupper函数
cout << str1 << endl;
cout << str2 << endl;
system("pause");
return 0;
}