C++的STL算法for_each
2017-03-13 09:10
465 查看
for_each()函数将调用者提供的操作施加于每一个元素身上。它既可以是非变动性算法,也可以说是变动性算法。
[cpp] view
plain copy
print?
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function f);
将函数f施加于区间[first,last)的每一个元素身上。其实现:
[cpp] view
plain copy
print?
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
for ( ; first!=last; ++first ) f(*first);
return f;
}
它返回f已在算法内部变动过的一个副本。
f可以是普通函数,也可是仿函数。它的任何返回值都将被忽略。
第三个参数在调用的时候可以是普通函数名;也可以说是函数对象,(定义重载operator的类)
程序实例:
下面的例子实现了两个功能:
一是使用普通函数print()打印所以元素;而是使用自定义的仿函数,即函数对象,来改变每个元素:将每个元素乘以3.
main.cpp:
[cpp] view
plain copy
print?
#include "algostuff.h"
using namespace std;
void print(int elem)
{
cout << elem << " ";
}
//define a functor
//multiply every element with the value initialized
template <class T>
class MultiplyValue{
private:
T value;
public:
MultiplyValue(const T val):value(val){}
//the function call
void operator()(T &elem)
{
elem *= value;
}
};
int main()
{
vector<int> intVec;
INSERT_ELEMENTS(intVec,1,9);
cout << "elements : " << endl;
for_each(intVec.begin(),intVec.end(),print);
cout << endl << endl;
for_each(intVec.begin(),intVec.end(),MultiplyValue<int>(3));
PRINT_ELEMNTS(intVec,"after multiply : ");
cout << endl;
}
algostuff.h:
[cpp] view
plain copy
print?
#ifndef ALGOSTUFF_H
#define ALGOSTUFF_H
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <functional>
#include <numeric>
//print all the elements
template <class T>
inline void PRINT_ELEMNTS(const T &col,const char *optcstr = " ")
{
typename T::const_iterator pos;
cout << optcstr;
for(pos = col.begin();pos != col.end();++pos)
cout << *pos << " ";
cout << endl;
}
//insert values from first to last into the collection
template <class T>
inline void INSERT_ELEMENTS(T &col,int first,int last)
{
for(int i = first;i <= last;++i)
col.insert(col.end(),i);
}
#endif
运行结果:
[cpp] view
plain copy
print?
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function f);
将函数f施加于区间[first,last)的每一个元素身上。其实现:
[cpp] view
plain copy
print?
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
for ( ; first!=last; ++first ) f(*first);
return f;
}
它返回f已在算法内部变动过的一个副本。
f可以是普通函数,也可是仿函数。它的任何返回值都将被忽略。
第三个参数在调用的时候可以是普通函数名;也可以说是函数对象,(定义重载operator的类)
程序实例:
下面的例子实现了两个功能:
一是使用普通函数print()打印所以元素;而是使用自定义的仿函数,即函数对象,来改变每个元素:将每个元素乘以3.
main.cpp:
[cpp] view
plain copy
print?
#include "algostuff.h"
using namespace std;
void print(int elem)
{
cout << elem << " ";
}
//define a functor
//multiply every element with the value initialized
template <class T>
class MultiplyValue{
private:
T value;
public:
MultiplyValue(const T val):value(val){}
//the function call
void operator()(T &elem)
{
elem *= value;
}
};
int main()
{
vector<int> intVec;
INSERT_ELEMENTS(intVec,1,9);
cout << "elements : " << endl;
for_each(intVec.begin(),intVec.end(),print);
cout << endl << endl;
for_each(intVec.begin(),intVec.end(),MultiplyValue<int>(3));
PRINT_ELEMNTS(intVec,"after multiply : ");
cout << endl;
}
algostuff.h:
[cpp] view
plain copy
print?
#ifndef ALGOSTUFF_H
#define ALGOSTUFF_H
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <functional>
#include <numeric>
//print all the elements
template <class T>
inline void PRINT_ELEMNTS(const T &col,const char *optcstr = " ")
{
typename T::const_iterator pos;
cout << optcstr;
for(pos = col.begin();pos != col.end();++pos)
cout << *pos << " ";
cout << endl;
}
//insert values from first to last into the collection
template <class T>
inline void INSERT_ELEMENTS(T &col,int first,int last)
{
for(int i = first;i <= last;++i)
col.insert(col.end(),i);
}
#endif
运行结果:
相关文章推荐
- c++_stl_for_each
- Effective Standard C++ Library: for_each() vs. transform()
- c++ STL之 for_each(权哥)
- 代码简洁之道:C++ 11 之auto+ for_each + lamda表达式
- C++ STL 学习 :for_each与仿函数(functor)(一)
- [C++]LeetCode: 123 Populating Next Right Pointers in Each Node(链接完全二叉树)
- [C/C++]類別互相引用(Class include each other)
- 【C++ STL学习之六】STL算法之for_each
- C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- 多想追求简洁的极致,但是无奈的学习C++中for_each的应用
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- C++ STL 学习 :for_each与仿函数(functor)
- [C/C++](Class include each other)
- C++ for_each函数\for_each模板
- 多想追求简洁的极致,但是无奈的学习C++中for_each的应用
- 【C++ STL学习之六】STL算法之for_each
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- [C/C++]類別互相引用(Class include each other)
- C++中for_each的应用
- C++ STL 学习 :for_each与仿函数(functor)