STL 之for_each,transform
2014-04-16 09:50
337 查看
返回
for_each:对指定区间中的每个元素使用指定的函数进行访问及处理,所用的函数作为参数传递给该函数。
transform:元素传输
声明:
示例代码:
运行结果:
charList:
a b c d e
charList:
A B C D E
list
2 8 5 1 7 11 3
4 16 10 2 14 22 6
list
4 16 10 2 14 22 6
for_each:对指定区间中的每个元素使用指定的函数进行访问及处理,所用的函数作为参数传递给该函数。
transform:元素传输
声明:
#include <algorithm> template <class inputItr,class function> function for_each(inputItr first,inputItr last,function func); template <class inputItr, class outputItr, class unaryOperation> outputItr transform(inputItr first,inputItr last, outputItr destFirst,unaryOperation op); template <class inputItr1, class inputItr2, class outputItr, class binaryOperation> outputItr transform(inputItr1 first1, inputItr1 last, inputItr2 first2, outputItr destFirst,binaryOperation bop);
示例代码:
#include <iostream> #include <list> #include <string> #include <numeric> #include <iterator> #include <vector> #include <functional> #include <algorithm> using namespace std; void doubleNum(int& num) { num = 2 * num; cout << num << " "; } int main() { char cList[5] = {'a','b','c','d','e'}; vector<char> charList(cList,cList+5); ostream_iterator<char> sreen(cout, " "); cout << "charList:" << endl; copy(charList.begin(),charList.end(),sreen); cout << endl; //transform transform(charList.begin(),charList.end(),charList.begin(),toupper); cout << "charList:" << endl; copy(charList.begin(),charList.end(),sreen); cout << endl; int list[7] = {2,8,5,1,7,11,3}; ostream_iterator<int> srceenInt(cout, " "); cout << "list" << endl; copy(list,list+7,srceenInt); cout << endl; // for_each for_each(list,list + 7, doubleNum); cout << endl; cout << "list" << endl; copy(list,list+7,srceenInt); cout << endl; return 0; }
运行结果:
charList:
a b c d e
charList:
A B C D E
list
2 8 5 1 7 11 3
4 16 10 2 14 22 6
list
4 16 10 2 14 22 6
相关文章推荐
- 记录 C++ STL 中 一些好用的函数--持续更新 (for_each,transform,count_if,find_if)
- STL遍历算法 “for_each ”,“transform”
- STL算法学习-- for_each( ) 与 transform()
- STL之bind2st for_each 和 transform
- STL_算法 for_each 和 transform 比较
- STL_Algorithm5-math: random_shuffle, count, count_if, min_element, max_element, accumulate, for_each, transform
- STL常用遍历算法for_each和transform的比较
- STL常用遍历算法for_each和transform的比较
- STL算法(19)——for_each()和transform()的比较
- STL算法 ------- transform() 与 for_each()比较
- C++ STL 中有关于for_each() 和 transform() 算法
- STL学习之十四:常用算法 for_each 和 transform以及adjacent_find的示例代码
- STL中的两个算法for_each与transform
- STL 之for_each,transform
- STL通用算法: for_each,c++11标准范围for,transform.
- stl for_each使用
- 【C++ STL学习之六】STL算法之for_each
- c++_stl_for_each
- C++ STL string 大小写转换时的 no matching function for call to ‘transform 错误
- STL之for_each, find_if,adjacent_find,count_if的使用