C++ examples for template:template function
Writing Your Own Algorithm
#include <iostream> #include <istream> #include <iterator> #include <string> #include <functional> #include <vector> #include <list> using namespace std; template<typename C> void printContainer(const C& c, char delim = ',', ostream& out = cout) { printRange(c.begin(), c.end(), delim, out); } template<typename Fwd> void printRange(Fwd first, Fwd last, char delim = ',', ostream& out = cout) { out << "{"; while (first != last) { out << *first;//from ww w .ja v a 2 s. c o m if (++first != last) out << delim << ' '; } out << "}" << endl; } template<typename In, typename Out, typename UnPred> Out copyIf(In first, In last, Out result, UnPred pred) { for ( ;first != last; ++first) if (pred(*first)) *result++ = *first; return(result); } int main() { cout << "Enter a series of strings: "; istream_iterator<string> start(cin); istream_iterator<string> end; // This creates a "marker" vector<string> v(start, end); list<string> lst; copyIf(v.begin(), v.end(), back_inserter<list<string> >(lst), bind2nd(less<string>(), "cookie")); printContainer(lst); }