使用类属性算法accumulate和multiplies计算连乘积
时间:2011-05-25 来源:孤独的猫
使用类属性算法accumulate和multiplies计算连乘积示例如下:
#include <iostream> #include <cassert> #include <algorithm> #include <list> #include <iterator> using namespace std; int main() { int a[10]={12,3,25,7,11,213,7,123,29,-31}; //int *ptr=find(&a[0],&a[10],7); //assert (*ptr==7 && *(ptr+1)==11); list<int> list1(&a[0],&a[10]); list<int>::iterator i=find(list1.begin(),list1.end(),7); assert (*i==7 && *(++i)==11); cout<<"Type some characters,including an 'x' followed\n"<<"by at least one nonwhite-space character: "<<flush; istream_iterator<char> in(cin); istream_iterator<char> eos; find(in,eos,'x'); cout<<"The first nonwhite-space character following\n"<<"the first 'x' was '"<<*(++in)<<"'."<<endl; return 0; } dongjichao@dongjichao:~/c$ cat ex02-14.cpp #include <iostream> #include <vector> #include <cassert> #include <numeric> #include <functional> using namespace std; int main() { cout<<"Using generic accumulate algorithm to compute a product."<<endl; int x[5]={2,3,5,7,11}; vector<int> vector1(&x[0],&x[5]); int product=accumulate(vector1.begin(),vector1.end(),1,multiplies<int>()); assert (product==2310); cout<<" --- Ok."<<endl; return 0; }
相关阅读 更多 +