transform原型:
std::transform
unary operation(1)
template <class InputIterator, class OutputIterator, class UnaryOperation>
OutputIterator transform (InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperation op);
|
binary operation(2)
template <class InputIterator1, class InputIterator2,
class OutputIterator, class BinaryOperation>
OutputIterator transform (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OutputIterator result,
BinaryOperation binary_op);
|
对指定范围中的每个元素调用某个函数以改变元素的值,存放到result的位置。
行为类似于:
template <class InputIterator, class OutputIterator, class UnaryOperator>
OutputIterator transform (InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperator op)
{
while (first1 != last1) {
*result = op(*first1); // or: *result=binary_op(*first1,*first2++);
++result; ++first1;
}
return result;
}
一个简单的例子:
// transform algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::transform
#include <vector> // std::vector
#include <functional> // std::plus
int op_increase (int i) { return ++i; }
void mtransform() {
std::vector<int> foo;
std::vector<int> bar;
// set some values:
for (int i=1; i<6; i++)
foo.push_back (i*10); // foo: 10 20 30 40 50
bar.resize(foo.size()); // allocate space
std::transform (foo.begin(), foo.end(), bar.begin(), op_increase);
// bar: 11 21 31 41 51
// std::plus adds together its two arguments:
std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>());
// foo: 21 41 61 81 101
std::cout << "foo contains:";
for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
运行结果:
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-26
于GDUT
——————————————————————————————————————————————————
分享到:
相关推荐
STL所有算法介绍STL所有算法介绍STL所有算法介绍
STL常用算法简介 STL常用算法简介 STL常用算法简介 STL常用算法简介 STL常用算法简介
stl常用算法,模板stl常用算法,
STL常用算法大全
参考自侯捷先生的《STL源码剖析》,C++ STL 的数值算法(Numeric algorithms)是一组对容器元素进行数值计算的模板函数,包括容器元素求和 accumulate 、两序列元素的内积 inner_product 、容器元素的一系列部分元素和...
STL和常用算法简介
algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明
ACM STL算法入门 导入 STL的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) Adaptors(配接器)
STL常用算法 七十个 12
自己根据编程原本与使用C++的经历写的STL的algorithm的实现,不好的地方请指出
STL切片算法,一种快速高效解决stl文件切片的程序算法讲解
C++ stl算法汇总 STL 各种算法 应用 大全
C++精选代码库。包含常用STL容器模拟实现、algorithm算法头文件函数demo
C++库函数及STL算法(英文版)介绍及用法 每个函数都有简单的源文件说明,有例子
STL算法部分主要由头文件<algorithm>,,组成。要使用 STL中的算法函数必须包含头文件<algorithm>,对于数值算法须包含,中则定义了一些模板类,用来声明函数对象。
该文档收录了STL中的部分常用的算法,非常实用。。。对于ACM编程着很大的帮助。。。
stl学习资料说明,stl基本使用说明,stl常用算法说明
关于stl的一些用法 STL算法作为模板函数提供 STL另一个重要特性是它不是面向对象的。为了具有足够通用性,STL主要依赖于模板而不是封装,继承和虚函数(多态性)——OOP的三个要素。
STL算法一览...............
STL算法(for_each/transform)