//第二十三模板 18.5数值算法 //STL的数值算法可以对一个序列的元素进行四种计算,接下来将逐步讲述这四种计算方法 //(1)accumulate(first,last,init) /*#include <iostream> #include <algorithm> #include <vector> #include <numeric> using namespace std; void show(int val) { cout<<val<<endl; } int main() { vector<int>one; for(int i=0; i<5; i++) { one.push_back(i); } for_each(one.begin(),one.end(),show); int result = accumulate(one.begin(),one.end()-1,5); cout<<"容器中前四个数加5的值为:"<<result<<endl; return 0; }*/ //(2)inner_product(first,last,first1,init) //首先计算first到last及first1所规定定的范围中元素的内积之和,内积是通过把每一个第一个序列中的元素与第二个序列中相同位置的元素相乘 //然后把结果相加而得到的,跟着将这个内积这和与init相加并返回, /*#include <iostream> #include <algorithm> #include <vector> #include <numeric> using namespace std; void show(int val) { cout<<val<<endl; } int main() { vector<int>one; for(int i=0; i<5; i++){ one.push_back(i); } vector<int>two; for(int i=0; i<10; i++){ two.push_back(i); } cout<<"one中的所有元素"<<endl; for_each(one.begin(),one.end(),show); cout<<"two中的所有元素"<<endl; for_each(two.begin(),two.end(),show); int result = inner_product(one.begin(),one.end(),two.begin(),0); cout<<"one的前几个元素与two的前几个元素做内积之和并与相中后的结果为:"<<result<<endl; return 0; }*/ //第二个容器的元素个数不要少于第一个容器的元素个数 //(3)partial_sum(first,last,result) /*#include <iostream> #include <algorithm> #include <vector> #include <numeric> using namespace std; void show(int val) { cout<<val<<endl; } int main() { vector<int>one; for(int i=0; i<5; i++){ one.push_back(i); } vector<int>two(5); partial_sum(one.begin(),one.end(),two.begin()); cout<<"one中的所有元素"<<endl; for_each(one.begin(),one.end(),show); cout<<"two中的所有元素"<<endl; for_each(two.begin(),two.end(),show); return 0; }*/ //(4) adjacent_difference(first,last,result) /*#include <iostream> #include <algorithm> #include <vector> #include <numeric> using namespace std; void show(int val) { cout<<val<<endl; } int main() { vector<int>one; for(int i=0; i<5; i++){ one.push_back(i*i); } vector<int>two(5); adjacent_difference(one.begin(),one.end(),two.begin()); cout<<"one中的所有元素"<<endl; for_each(one.begin(),one.end(),show); cout<<"two中的所有元素"<<endl; for_each(two.begin(),two.end(),show); return 0; }*/ //总结 /* 非模板友元函数 通用模析板友元函数 特定类型友元函数 1: 顺序容器 向量容器 vector 列表容器 lists 双向队列容器 deque 队列容器 queue 优先队列 Priority Queues 堆栈类 queue 2: 关联容器 映射 map 集合 set 多重映射 Multimaps 生重集合 MultiSets 位集合 Bitsets 3: 算法类 不变序列算法 不对其所作用的容器进行修改 改变序列算法 对其所作用的容器进行修改 排序算法 对容器中的元素采用多种方式进行排序 数值算法 对容器中的元素进行数值计算 */