/////////////////////////////////////////////////////////////////////////// // inserter.cpp - Demonstrate inserter iterators and Transform Algorithm // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2005 // /////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include #include #include #include #include #include using namespace std; template void show(cont &c) { cont::iterator it; cout << "\n "; cout.setf(ios::showpoint); cout.precision(3); for(it=c.begin(); it!=c.end(); ++it) cout << setw(5) << (*it) << " "; cout << "\n\n"; } double square(double d) { return (d*d); } int _tmain(int argc, _TCHAR* argv[]) { // Transform applies a specified function object to each element // in a source range and copies the return values of the function // object into a destination range. double darray[6] = { 0.5, 1.0, 1.5, 2.0, 2.5, 3.0 }; list ld(&darray[0],&darray[5]); show(ld); // front_inserter and back_inserter are pre-defined iterators vector squares; transform(ld.begin(),ld.end(),back_inserter(squares),square); show(squares); transform(ld.rbegin(),ld.rend(),front_inserter(ld),square); show(ld); return 0; }