// MT2Q8.cpp #include #include #include template void show(const C& c) { std::cout << "\n "; C::const_iterator iter; for(iter=c.begin(); iter!=c.end(); ++iter) std::cout << (*iter).c_str() << " "; } template struct IFunctor { virtual bool operator()(T& t)=0; virtual ~IFunctor() {} }; template class SampleFunctor : public IFunctor { public: SampleFunctor(char chr='A') : ch(chr) {} bool operator()(T& t) { return (t[0] == ch) ? true : false; } private: char ch; }; template void copy_ifeach(U& u, V& v, IFunctor& f) { U::iterator iter; for(iter=u.begin(); iter!=u.end(); ++iter) if(f(*iter)) v.push_back(*iter); } void main() { std::string names[] = { "Arjun", "Bill", "Priya", "Aaron", "John", "Yu" }; std::list ls(&names[0],&names[6]); show(ls); std::vector vs; copy_ifeach(ls,vs,SampleFunctor()); show(vs); copy_ifeach(ls,vs,SampleFunctor('Y')); show(vs); std::cout << "\n\n"; }