template , for_each , fill

来源:互联网 发布:网络招商 编辑:程序博客网 时间:2024/05/01 14:29

#include <iostream> #include <vector> #include <algorithm> using namespace std; template <class T> class Print {     public:         void operator () (T& t)         {             cout << t << " ";         } }; int main () {     vector<int> v(10);     Print<int> print;     fill(v.begin(),v.end(),5);     cout << "Vector v : ";     for_each(v.begin(),v.end(),print);     cout << endl;     cout << "Size of v = " << v.size() << endl;     cout << "v.clear" << endl;     v.clear();     cout << "Vector v : ";     for_each(v.begin(),v.end(),print);     cout << endl; cout << "Size of v = " << v.size() << endl;     cout << "Vector v is ";     v.empty() ? cout << "" : cout << "not "; cout << "empty" << endl;     return 0; } // Vector v : 5 5 5 5 5 5 5 // Size of v = 10 // v.clear // Vector v : // Size of v = 0 // Vector v is empty 



code:




原创粉丝点击