C++ 的 std::transform
函数是 <algorithm> 头文件中的一个通用算法 , 用于对指定范围内的元素进行转换 ;
std
命令空间 中的 transform
函数 用于对 STL 容器 指定范围的内容进行转换 ;
根据提供的参数 , 该函数可以从源字符串中提取字符 , 并按照指定的格式进行转换 , 然后返回一个新的字符串 ;
template< class InputIt, class OutputIt, class UnaryOperation >
OutputIt transform( InputIt first, InputIt last, OutputIt d_first, UnaryOperation unary_op );
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation >
OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op );
参数说明 :
操作函数对象 :
代码示例 :
#include "iostream"
using namespace std;
#include "string"
#include "algorithm"
int main() {
string s1 = "Tom And Jerry";
// 将字符串转为大写字母
transform(s1.begin(), s1.end(), s1.begin(), toupper);
// 打印 s1值
cout << "s1 = " << s1 << endl;
// 将字符串转为小写字母
transform(s1.begin(), s1.end(), s1.begin(), tolower);
// 打印 s1值
cout << "s1 = " << s1 << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
执行结果 :
s1 = TOM AND JERRY s1 = tom and jerry 请按任意键继续. . .
std::reverse
是 <algorithm> 头文件中 的一个 算法函数 , 用于反转给定 STL 容器 范围内的元素的顺序 ;
std::reverse
函数原型 :
template< class BidirectionalIt >
void reverse( BidirectionalIt first, BidirectionalIt last );
参数说明:
代码示例 :
#include "iostream"
using namespace std;
#include "string"
#include "algorithm"
int main() {
string s1 = "Tom And Jerry";
// 将字符串 翻转
reverse(s1.begin(), s1.end());
// 打印 s1值
cout << "s1 = " << s1 << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
执行结果 :
s1 = yrreJ dnA moT 请按任意键继续. . .