#include<iostream> #include<vector> #include<functional> #include<algorithm> using namespace std; //遍历函数 for_each //普通函数 void print1(int val) { cout << val << " "; } //仿函数 class print2 { public: void operator()(int val) { cout << val << " "; } }; void test1() { vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << " "; } cout << endl; for_each(v.begin(), v.end(), print1); cout << endl; for_each(v.begin(), v.end(), print2()); } int main() { test1(); return 0; }
标签:遍历,cout,back,C++,each,push,include From: https://www.cnblogs.com/ggbond00/p/17201068.html