ccplusplus.com
Learn C, C++ Concepts
Thursday, January 12, 2012
using function objects stl
/*********************************************************************** * File : for-each.cpp * Author : Saurabh Gupta * Desc : using function objects stl * Source : http://ccplusplus/2011/10/standard-template-library.html * Created : PM 11:04 12 January 2012 * Note : ************************************************************************/ #include <iostream> #include <vector> #include <algorithm> using namespace std; //simple function object that prints the passed argument class PrintInt { public: void operator() (int elem) const { cout << elem << ' '; } }; int main() { vector<int> coll; //insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll.push_back(i); } //print all elements for_each (coll.begin(), coll.end(), //range PrintInt()); //operation cout << endl; } /* * OUTPUT * [sgupta@rhel6x64 function-object]$ c++ for-each.cpp -o for-each [sgupta@rhel6x64 function-object]$ ./for-each 1 2 3 4 5 6 7 8 9 [sgupta@rhel6x64 function-object]$ */
See Also:
Standard Template Library Concepts and sample codes in C++
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment