`
mywebcode
  • 浏览: 1003737 次
文章分类
社区版块
存档分类
最新评论

STL 之for_each,transform

 
阅读更多

返回


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

分享到:
评论

相关推荐

    STL算法(for_each/transform)

    STL算法(for_each/transform)

    计算科学-第一次实验-报告及修改后的代码包

    1. 把矩阵向量相乘示例中的for_each(),for_each_n(),generate(),transform()等stl库函数调用改写成普通for循环。测试算例。 2. 保留上面的stl库函数调用,但添加execution的执行策略,测试并行的效果。 3. 调用...

    C++ STL开发技术导引(第5章)

    21.1 逐个容器元素for_each 284 21.2 查找容器元素find 285 21.3 条件查找容器元素find_if 286 21.4 邻近查找容器元素adjacent_find 287 21.5 范围查找容器元素find_first_of 289 21.6 统计等于某值的...

    STL源码剖析.pdg

    for_each 348 generate 349 generate_n 349 includes (应用于有序区间) 349 max_element 352 merge (应用于有序区间) 352 min_element 354 partition 354 remove 357 remove_copy 357 remove_if 357 ...

    javaparser:基于函数式组合子逻辑的JAVA语言分析框架

    一个c++程序员所熟悉的“函数式”很可能是stl的for_each, transform,count_if这些函数。 怎么说呢,就象我不能否定str.length()这个调用属于OO一样,我也无法说for_each, transform不是函数式。 但是,“函数式”的...

    STL算法详解与汇总

    STL将算法库分成4组: 1)非修改式序列操作,find()、for_each()等; 2)修改式序列操作,transform()、random_shuffle()、copy等; 3)排序和相关操作,sort()等; 4)通用数字运算。 ........

    C++标准程序库STL的架构

    5.8.1 示例for_each和transform 29 5.8.2 判断式(predicates) 30 5.9 仿函数 33 5.9.1 什么是仿函数 33 5.9.2 预先定义的仿函数 35 5.10 容器内的元素&lt;class T&gt; 36 5.10.1 容器元素的条件 36 5.10.2 value和...

    STL 源码剖析(侯捷先生译著)

    for_each 348 generate 349 generate_n 349 includes (应用于有序区间) 349 max_element 352 merge (应用于有序区间) 352 min_element 354 partition 354 remove 357 remove_copy 357 remove_if 357 ...

    C++ STL 开发技术导引(第6章)

    21.1 逐个容器元素for_each 284 21.2 查找容器元素find 285 21.3 条件查找容器元素find_if 286 21.4 邻近查找容器元素adjacent_find 287 21.5 范围查找容器元素find_first_of 289 21.6 统计等于某值的...

    C++ STL开发技术导引(第3章)

    21.1 逐个容器元素for_each 284 21.2 查找容器元素find 285 21.3 条件查找容器元素find_if 286 21.4 邻近查找容器元素adjacent_find 287 21.5 范围查找容器元素find_first_of 289 21.6 统计等于某值的...

    C++Primer视频(中级)下载地址

    第11章 算法 比较for_each和transform 40.第11章 算法 交换算法 41.第11章 算法 填充新值 42.第11章 算法 替换算法 43.第11章 算法 删除算法 (1) 44.第11章 算法 删除算法 (2) 45.第11章 算法 删除算法 ...

    SGI-STL 源码以及 word 注解版

    Function for_each (InputIterator first, InputIterator last, Function f) { while (first != last) f(*first++); return f; } template , class T&gt; InputIterator find (InputIterator first, ...

    -C++参考大全(第四版) (2010 年度畅销榜

    34.14 for_each 34.15 generate和generate_n 34.16 includes 34.17 inplace_merge 34.18 iter_swap 34.19 lexicographical_compare 34.20 lower_bound 34.21 make_heap 34.22 max 34.23 max_element 34.24 merge 34....

    通过代码实例解析c++ vector常用方法

    1. c++ vector 每个元素加上一个特定值 (c++ vector add a constant value for each element) https://stackoverflow.com/questions/4461446/stl-way-to-add-a-constant-value-to-a-stdvector vector&lt;int&gt; x = {0, ...

    Visual C++ 2010入门经典(第5版)--源代码及课后练习答案

    3.2.2 for循环的变体 119 3.2.3 while循环 126 3.2.4 do-while循环 128 3.2.5 嵌套的循环 129 3.3 C++/CLI编程 132 3.4 小结 137 3.5 练习 138 3.6 本章主要内容 138 第4章 数组、字符串和指针 139 4.1 ...

Global site tag (gtag.js) - Google Analytics