STL (1)for_each 函数
2013-11-22 10:19
375 查看
/* // TEMPLATE FUNCTION for_each // template<class _InIt, class _Fn1> inline _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func) { // perform function for each element _DEBUG_RANGE(_First, _Last); _DEBUG_POINTER(_Func); _CHECKED_BASE_TYPE(_InIt) _ChkFirst(_CHECKED_BASE(_First)); _CHECKED_BASE_TYPE(_InIt) _ChkLast(_CHECKED_BASE(_Last)); for (; _ChkFirst != _ChkLast; ++_ChkFirst) //利用_ChkFirst移动迭代器 _Func(*_ChkFirst); //每个元素都执行函数_Func return (_Func); } */ //*********************by vincent http://my.csdn.net/sunboyiris ************************// #include "stdafx.h" #include "algorithm" #include "list" #include "iostream" using namespace std; struct print { int count; print(){count=0;} void operator()(int x) { cout<<x<<endl; count++; } }; void print1(int& i) { cout<<i<<endl; } void print2(int i,const char* ptr) { cout<<ptr<<" : "<<i<<endl; } int _tmain(int argc, _TCHAR* argv[]) { list<int> l; for(int i=0;i<10;i++) { l.push_back(i); } //引用指针 int count1=0; list<int>::iterator iter; for(iter=l.begin();iter!=l.end();iter++) { count1++; } cout<<count1<<endl; //----------------不传入参数--------// print p=for_each(l.begin(),l.end(),print()); cout<<"the number is :"<<p.count<<endl; //----------------不传入参数--------// for_each(l.begin(),l.end(),print1); //----------------传入参数--------// for_each(l.begin(),l.end(),bind2nd(ptr_fun(print2),"the number is ")); return 0; }
相关文章推荐
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- STL中的for_each调用类成员函数,mem_fun, mem_fun_ref用法
- 记录 C++ STL 中 一些好用的函数--持续更新 (for_each,transform,count_if,find_if)
- STL:从for_each到函数对象再到bind函数
- 【STL】函数 for_each; bind1st和bind2nd,not1; mem_fun和mem_fun_ref;
- STL for_each 和函数对象(function object) 使用详解
- STL中的for_each()函数
- STL —— for_each与仿函数、函数指针
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- STL::for_each用法
- (原創) 如何使用for_each() algorithm? (C/C++) (STL)
- for_each如何调用全局的和类的成员函数举例
- STL_算法_for_each
- for_each各种情况下的函数对象使用方法
- (原創) 如何正確的使用迴圈(使用for_each)? (C/C++) (STL) (template)
- for_each()在函数对象中的独特作用
- STL算法 ------- transform() 与 for_each()比较
- (STL)for_each、find_if用法
- C++ STL 中有关于for_each() 和 transform() 算法
- C++ STL 学习 :for_each与仿函数(functor)(一)