c++常用函数

1、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。

注意:第二个重载版本必须要保证两个容器的元素个数相等才行,否则会抛出异常。
看一个例子:利用transform函数将一个给定的字符串中的小写字母改写成大写字母,并将结果保存在一个叫second的数组里,原字符串内容不变。

string  paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.";

transform(paragraph.begin(),paragraph.end(),paragraph.begin(),::tolower); //将paragraph中字符转换成小写

原文地址:https://www.cnblogs.com/sunshine1218/p/12601350.html