运用数值算法之前必须先加入头文件<numeric>
加工运算后产生结果
1.对序列进行某种运算
T
accumulate(InputIterator beg,InputIterator end,
T initValue)
T
accumulate(InputIterator beg,InputIterator end,
T initValue,BinaryFunc op)
1.第一种形式计算InitValue和区间[beg,end)内所有元素的总和。
2.第二种形式计算initValue和区间[beg,end)内每一个元素进行op运算的结果。更具体的说,它针对每一个元素调用以下表达式:
initValue=op(initValue,elem)
下面这个例子展示如何使用accumulate()得到区间内所有元素的总和和乘积:
1 #include "algostuff.hpp" 2 using namespace std; 3 4 int main() 5 { 6 vector<int> coll; 7 INSERT_ELEMENTS(coll,1,9); 8 PRINT_ELEMENTS(coll); 9 cout<<"sum: " 10 <<accumulate(coll.begin(),coll.end(),0) 11 <<endl; 12 cout<<"sum: " 13 <<accumulate(coll.begin(),coll.end(),-100) 14 <<endl; 15 cout<<"product: " 16 <<accumulate(coll.begin(),coll.end(),1,multiplies<int>()) 17 <<endl; 18 cout<<"product: " 19 <<accumulate(coll.begin(),coll.end(),0,multiplies<int>()) 20 <<endl; 21 }