ccplusplus.com
Learn C, C++ Concepts
Friday, May 4, 2012
predefined function objects example c++
/******************************************************************* * File : predefined-function-object.cpp * Author : Team ccplusplus * Description : predefined function objects example c++ * Date : PM 07:38 04 May 2012 * Source : http://www.ccplusplus.com/p/c_15.html * Note : *******************************************************************/ #include
#include
#include
#include
using namespace std; /* * - printElement() * - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */ template
inline void printElement (const T& coll, const char* optcstr="") { typename T::const_iterator pos; cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { cout << *pos << ' '; } cout << endl; } int main() { set
> coll1; deque
coll2; //insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll1.insert(i); } printElement(coll1,"initialized: "); //transform all elements into coll2 by multiplying 10 transform (coll1 .begin(), coll1 .end(), //source back_inserter(coll2), //destination bind2nd(multiplies
() ,10)); //operation printElement(coll2,"transformed: "); //replace value equal to 70 with 42 replace_if (coll2.begin(),coll2.end(), //range bind2nd(equal_to
() ,70) , //replace criterion 42) ; //new value printElement(coll2,"replaced: "); //remove all elements with values less than 50 coll2.erase(remove_if(coll2.begin(),coll2.end(), //range bind2nd(less
() ,50)), //remove criterion coll2.end()); printElement(coll2,"removed: "); return 0; } /* * OUTPUT * [blog@ccplusplus function-object]$ c++ predefined-function-object.cpp -o predefined-function-object [blog@ccplusplus function-object]$ ./predefined-function-object initialized: 9 8 7 6 5 4 3 2 1 transformed: 90 80 70 60 50 40 30 20 10 replaced: 90 80 42 60 50 40 30 20 10 removed: 90 80 60 50 [blog@ccplusplus function-object]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment