#include <iostream> #include <vector> #include <iterator> #include <algorithm> using namespace std; void output(int i) { cout << "i= " << i << "\t"; } class stroutput { public: void operator()(int i ) { cout << "i= " << i << "\t"; } }; struct mystr { void operator()(int i) { cout << "i= " << i << "\t"; } }myobj; bool great20(int i) { if(i> 20) { return true; } return false; } int setnum() { static int i = 0; return i++; } int main(int argc,char* argv[]) { vector<int> vec; vec.push_back(10); vec.push_back(20); vec.push_back(30); vec.push_back(40); vec.push_back(10); vec.push_back(20); vec.push_back(30); vec.push_back(40); vec.push_back(50); vector<int> vec2(vec.begin(),vec.end()--); //for_each //for_each(vec.begin(),vec.end(),output); //for_each(vec.begin(),vec.end(),stroutput()); //for_each(vec.begin(),vec.end(),myobj); vector<int>::iterator it; //find /*vector<int>::iterator it = find(vec.begin(),vec.end(),20); it++; cout << "*it = " << *it << endl; */ //find_if // it = find_if(vec.begin(),vec.end(),great20); // cout << "*it = " << *it << endl; //find_first_of //int arr[] = {40,30}; //it = find_first_of(vec.begin(),vec.end(),arr,arr+2); //count //int num = count(vec.begin(),vec.end(),10); //count_if int num = count_if(vec.begin(),vec.end(),great20); cout << "num = " << num << endl; //equal bool b = equal(vec.begin(),vec.end(),vec2.begin()+1); cout << "b=" << b << endl; //replace //replace(vec.begin(),vec.end(),20,100); // for_each(vec.begin(),vec.end(),output); vector<int> vec3(vec.size()); replace_copy(vec.begin(),vec.end(),vec3.begin(),20,100); // for_each(vec3.begin(),vec3.end(),output); fill(vec3.begin(),vec3.end(),5); for_each(vec3.begin(),vec3.end(),output); vector<int> vec4(10); generate(vec4.begin(),vec4.end(),setnum); for_each(vec4.begin(),vec4.end(),output); return 0; }
for_each
最新推荐文章于 2024-02-26 11:41:26 发布