-
算术生成算法属于小型算法,使用时包含的头文件为 #include
-
accumulate(iterator beg, iterator end, value);
// 计算容器元素累计总和
// beg 开始迭代器
// end 结束迭代器
// value 起始值
-
fill(iterator beg, iterator end, value);
// 向容器中填充元素
// beg 开始迭代器
// end 结束迭代器
// value 填充的值
#include#include #include #include #include //包含小型算法头文件 using namespace std; class print { public: void operator()(int v1) { cout << v1 << " "; } }; void test01()//accumulate { vector v; for (int i = 0; i < 10; i++) { v.push_back(i); } for_each(v.begin(), v.end(), print()); cout << endl; int sum = accumulate(v.begin(), v.end(), 10);//最后一个参数为起始值,从该值往上累加容器里边所有元素的和 cout << sum << endl; } void test02() { vector v; v.push_back(2); v.push_back(4); v.push_back(6); v.push_back(8); v.push_back(4); v.push_back(6); for_each(v.begin(), v.end(), print()); //后期重新填充 fill(v.begin(), v.end(), 100);//把容器内区间的元素用100来填充 cout << endl; for_each(v.begin(), v.end(), print()); } int main() { //test01();//accumulate test02();//fill return 0; }



