STL —— for_each与仿函数、函数指针
2012-03-26 17:33
465 查看
#include <iostream> #include <vector> #include <algorithm> using namespace std; class PrintInt { public: void operator() (int elem) const { cout << elem << '\t'; } }; class PrintInt2 { public: PrintInt2(int x) : m_x(x) { } void operator() (int elem) const { cout << elem + m_x << '\t'; } private: int m_x; }; typedef void (*op)(int &elem); void Func(int &elem) { cout << 2*elem << '\t'; } int main(void) { vector<int> coll; op p = Func; for (int i=1; i<=9; i++) { coll.push_back(i); } for_each(coll.begin(), coll.end(), PrintInt()); cout << endl; for_each(coll.begin(), coll.end(), PrintInt2(10)); cout << endl; for_each(coll.begin(), coll.end(), Func); cout << endl; for_each(coll.begin(), coll.end(), p); cout << endl; return 0; }
相关文章推荐
- 浅析STL 谓词 + 仿函数 + 函数指针(c)
- STL中的for_each()函数
- 记录 C++ STL 中 一些好用的函数--持续更新 (for_each,transform,count_if,find_if)
- STL for_each 和函数对象(function object) 使用详解
- STL:从for_each到函数对象再到bind函数
- C++ STL 学习 :for_each与仿函数(functor)(一)
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- STL的适配器、仿函数学习之一:accumulate和for_each的使用心得
- C++ STL 学习 :for_each与仿函数(functor)(一)
- STL (1)for_each 函数
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- C++ STL 学习 :for_each与仿函数(functor)
- 【STL】函数 for_each; bind1st和bind2nd,not1; mem_fun和mem_fun_ref;
- STL中的for_each调用类成员函数,mem_fun, mem_fun_ref用法
- C++ STL 学习 :for_each与仿函数(functor)
- STL-fill()填充算法以及for_each()算法
- 成员函数做for_each 参数
- STL常用遍历算法for_each和transform的比较
- C++ STL 学习 :for_each与仿函数(functor)
- for_each 函数 in STL