// wwww.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <list> #include <vector> #include <algorithm> #include <functional> #include <numeric> using namespace std; //博客地址:http://www.cnblogs.com/lvpengms/archive/2010/02/03/1663078.html class Even { public: bool operator()(int a) { return (a%2 == 0); } }; int _tmain(int argc, _TCHAR* argv[]) { int arr[100]; generate_n(arr, 10, rand); copy(arr, arr+10, ostream_iterator<int>(cout, " ")); cout<<endl; cout<<"max = "<<*max_element(arr, arr+100)<<endl; fill_n(arr, 10, 0); copy(arr, arr+10, ostream_iterator<int>(cout, " ")); cout<<endl; list<int> l; vector<int> v; generate_n(back_inserter(l), 10, rand); copy(l.begin(), l.end(), ostream_iterator<int>(cout, " ")); cout<<endl; copy(l.begin(), l.end(), back_inserter(v)); copy(v.begin(), v.end(), ostream_iterator<int>(cout, " ")); cout<<endl; //partition 和stable_partition(对序列进行分割) int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int N = sizeof(A) / sizeof(int); partition(A, A+N, Even()); //不稳定分割 copy(A, A+N, ostream_iterator<int>(cout, " ")); cout<<endl; stable_partition(A, A+N, Even());//稳定分割(保持元素的顺序不变) copy(A, A+N, ostream_iterator<int>(cout, " ")); cout<<endl; random_shuffle(A, A+N);//随机重排,N个元素的排列有N!种可能,random_shuffle会产生一个均匀分布,任何特定的排列被选中的概率为1/N! copy(A, A+N, ostream_iterator<int>(cout, " ")); cout<<endl; cout<<"The sum of arr A:"<<accumulate(A, A+N, 0)<<endl; fill(A, A+N, 1); partial_sum(A, A+N, ostream_iterator<int>(cout, " "));// 用来计算部分和 cout<<endl; adjacent_difference(A, A+N, ostream_iterator<int>(cout, " "));//计算相邻两个元素的差值 cout<<endl; generate_n(A, 10, rand); sort(A, A+N);//排序 stable_sort(A, A+N);//稳定排序 partial_sort(A, A+5, A+N);//部分排序,保证前5个元素递增,后5个元素的顺序不确定 copy(A, A+N, ostream_iterator<int>(cout, " ")); cout<<endl; //迭代器类 //front_insert_iterator list<int> L; L.push_front(3); front_insert_iterator<list<int> > ii(L); *ii++ = 0; *ii++ = 1; *ii++ = 2; copy(L.begin(), L.end(), ostream_iterator<int>(cout, " ")); cout<<endl; L.clear(); v.clear(); for (int i=0; i<10; i++) { v.push_back(i+1); } copy(v.begin(), v.end(), front_inserter(L));//将v中元素反向存入L中 copy(L.begin(), L.end(), ostream_iterator<int>(cout, " ")); cout<<endl; //back_insert_iterator //insert_iterator //流迭代器(Stream iterators) //istream_iterator v.clear(); copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(v)); //ostream_iterator copy(v.begin(), v.end(), ostream_iterator<int>(cout, " ")); cout<<endl; //istreambuf_iterator 从输入流中读取单个字符 istreambuf_iterator<char> first(cin); istreambuf_iterator<char> end_of_stream; vector<char> buffer(first, end_of_stream); //ostreambuf_iterator 向输出流中输入单个字符 string str = "This is a test!/n"; copy(str.begin(), str.end(), ostreambuf_iterator<char>(cout)); //reverse_iterator 反向迭代器 //raw_storage_iterator //函数对象 //unary_function 一元函数对象的基类 //binary_function 二元函数对象的基类 //算术运算 //plus 求和 transform(l.begin(), l.end(), v.begin(), L.begin(), plus<int>()); //minus 求差 //multiplies 求乘 //divides 求除 return 0; } // wwww.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <list> #include <vector> #include <algorithm> #include <functional> #include <numeric> using namespace std; struct B { virtual void print() = 0; }; struct D1 : public B { void print() { cout<<"I am a D1"<<endl; } }; struct D2 : public B { void print() { cout<<"I am a D2"<<endl; } }; int main() { vector<B*> v1; v1.push_back(new D1); v1.push_back(new D2); for_each(v1.begin(), v1.end(), mem_fun(&B::print)); vector<D1> v2; v2.push_back(D1()); v2.push_back(D1()); for_each(v2.begin(), v2.end(), mem_fun_ref(&B::print)); /* mem_fun_ref的作用和用法跟mem_fun一样,唯一的不同就是:当容器中存放的是对象实体的时候用mem_fun_ref, 当容器中存放的是对象的指针的时候用mem_fun。 */ }