for_each用法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了for_each用法相关的知识,希望对你有一定的参考价值。
for_each()是个function template #include <algorithm>头文件说明 template<class _InIt, class _Fn1> inline void _For_each(_InIt _First, _InIt _Last, _Fn1& _Func) { // perform function for each element for (; _First != _Last; ++_First) _Func(*_First); } 由以上source可知,for_each()只能配合global function和function object。 #include <iostream> #include <stdlib.h> #include "vector" #include "map" #include <algorithm> #include <atlstr.h> #include <xfunctional> using namespace std; //单一类对象 class CClassObj { public: CClassObj(void){} ~CClassObj(void){} void PrintElem(){ std::cout << "mem_fun_ref" << std::endl; } }; class ClassController { protected: vector<CClassObj> _ObjVec; public: ClassController(void){} ~ClassController(void){} void AddClass(CClassObj cObj){ _ObjVec.push_back(cObj); } void PrintElem(){ for_each(_ObjVec.begin(), _ObjVec.end(), mem_fun_ref(&CClassObj::PrintElem)); } }; //多态 class AbstactClass { public: virtual void PrintElem(){ std::cout << "Abstact mem_fun 1" << std::endl; } }; class MyClass1 : public AbstactClass{}; class MyClass2 : public AbstactClass { public: void PrintElem(){ std::cout << "Abstact mem_fun 2" << std::endl; } }; class ClassCont { protected: vector<AbstactClass*> _ObjVec; public: void AddClass(AbstactClass& obj){ _ObjVec.push_back(&obj); } void PrintElem(){ for_each(_ObjVec.begin(), _ObjVec.end(), mem_fun(&AbstactClass::PrintElem)); } }; struct Mystruct { int cnt; string str; Mystruct() { cnt = 0; str = "str"; } }; //函数调用对象 for_each()只能配合global function和function object。 /*void Print(const Mystruct stru){ cout << stru.cnt << ", " << stru.str.c_str() << endl; }*/ void Print(const Mystruct stru, const char* str){ cout << str << stru.cnt << ", " << stru.str.c_str() << endl; } struct PrintStru { string _str = ""; PrintStru(string str) : _str(str){ } void operator()(string &str){ cout << _str.c_str() << str.c_str() << endl; } }; int main() { /* Add a(1, 2), b(3, 4);*/ /* std::cout << a << " + " << b << " = " << (a + b) << std::endl;*/ vector<Mystruct> vecStru; Mystruct stru; vecStru.push_back(stru); vecStru.push_back(stru); /*for_each(vecStru.begin(), vecStru.end(), [](const Mystruct stru){cout << stru.cnt << ", " << stru.str.c_str() << endl; });//lambda可调用对象*/ //Procedure Based與for_each()搭配 /*for_each(vecStru.begin(), vecStru.end(), Print);//print: 不传入参数*/ for_each(vecStru.begin(), vecStru.end(), bind2nd(ptr_fun(Print), "elem: "));//print: 传入参数 //Object Oriented與for_each()搭配 string Arr[] = { "123", "123456", "12345", "12", "1" }; vector<string> vecStr(Arr, Arr + sizeof(Arr) / sizeof(string)); for_each(vecStr.begin(), vecStr.end(), PrintStru(""));//print: 不传入参数 //若使用function object,也可以將參數傳給PrintStru(),透過constructor的技巧接收參數。 for_each(vecStr.begin(), vecStr.end(), PrintStru("Elem: "));//print: 不传入参数 ClassController cConT; cConT.AddClass(CClassObj()); cConT.AddClass(CClassObj()); cConT.PrintElem(); //多态 ClassCont cT; cT.AddClass(MyClass1()); cT.AddClass(MyClass2()); cT.PrintElem(); }
借鉴:http://www.cnblogs.com/Purple_Xiapei/archive/2012/05/27/2520477.html
以上是关于for_each用法的主要内容,如果未能解决你的问题,请参考以下文章