您的位置:首页 > 其它

STL学习笔记----16.STL算法之 (数值算法)

2013-10-17 14:39 405 查看
一. 概述

用来处理数值的算法,需要加上头文件 #include<numberic>

accumulate()组合所有元素(求总和,求乘积...)
inner_product()组合两区间内的所有元素
adjacent_difference()将每个元素和其前一元素组合
partial_sum()将每个元素和其先前的所有元素组合
二. 加工运算后产生结果

1. 对序列进行某种运算

[cpp]
view plaincopyprint?

//计算initValue和区间[beg, end)内所有元素的总和 
//也就是: initValue + a1 + a2 + a3 +... 

accumulate (InputIterator beg, InputIterator end,  
            T initValue) 
 
//也就是:initValue op a1 op a2 ... 
//比如 op 是 乘,则为:initValue * a1 * a2 * a3... 

accumulate (InputIterator beg. InputIterator end,  
            T initValue, BinaryFunc op) 

//计算initValue和区间[beg, end)内所有元素的总和
//也就是: initValue + a1 + a2 + a3 +...
T
accumulate (InputIterator beg, InputIterator end,
T initValue)

//也就是:initValue op a1 op a2 ...
//比如 op 是 乘,则为:initValue * a1 * a2 * a3...
T
accumulate (InputIterator beg. InputIterator end,
T initValue, BinaryFunc op)

2. 计算两序列内积

[cpp]
view plaincopyprint?

//返回[beg, end)区间和beg2为起始的区间的对应元素组的内积 
//initValue + (a1*b1) + (a2*b2) + (a3*b3) + ... 

inner_product (InputIterator1 beg1, InputIterator1 end1,  
               InputIterator2 beg2,  
               T initValue) 
 
//initValue op1 (a1 op2 b1) op1 (a2 op2 b2) op1 ... 

inner_product (InputIterator1 beg1. InputIterator1 end1,  
               InputIterator2 beg2,  
               T initValue,  
               BinaryFunc op1, BinaryFunc op2) 

//返回[beg, end)区间和beg2为起始的区间的对应元素组的内积
//initValue + (a1*b1) + (a2*b2) + (a3*b3) + ...
T
inner_product (InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2,
T initValue)

//initValue op1 (a1 op2 b1) op1 (a2 op2 b2) op1 ...
T
inner_product (InputIterator1 beg1. InputIterator1 end1,
InputIterator2 beg2,
T initValue,
BinaryFunc op1, BinaryFunc op2)

三. 相对值和绝对值之间的转换

1. 将相对值转换成绝对值

[cpp]
view plaincopyprint?

//计算区间[sourceBeg, sourceEnd)中每个元素的部分和,然后写入destBeg 
//a1, a1+a2, a1+a2+a3, ... 
OutputIterator 
partial_sum (InputIterator sourceBeg, InputIterator sourceEnd,  

             OutputIterator destBeg) 
 
//a1, a1 op a2, a1 op a2 op a3, ... 
OutputIterator 
partial_sum (InputIterator sourceBeg, InputIterator sourceEnd,  
             OutputIterator destBeg, BinaryFunc op) 

//计算区间[sourceBeg, sourceEnd)中每个元素的部分和,然后写入destBeg
//a1, a1+a2, a1+a2+a3, ...
OutputIterator
partial_sum (InputIterator sourceBeg, InputIterator sourceEnd,
OutputIterator destBeg)

//a1, a1 op a2, a1 op a2 op a3, ...
OutputIterator
partial_sum (InputIterator sourceBeg, InputIterator sourceEnd,
OutputIterator destBeg, BinaryFunc op)

2. 将绝对值转换成相对值

[cpp]
view plaincopyprint?

//计算区间[sourceBeg, sourceEnd)中每一个元素前趋差,然后写destBeg 
//a1, a2-a1, a3-a2, a4-a3, ... 

OutputIterator 
adjacent_difference (InputIterator sourceBeg,  
                     InputIterator sourceEnd,  
                     OutputIterator destBeg) 
 
//a1, a2 op a1, a3 op a2, a4 op a3, ... 
OutputIterator 
adjacent_difference (InputIterator sourceBeg,  
                     InputIterator sourceEnd,  
                     OutputIterator destBeg,  
                     BinaryFunc op) 

//计算区间[sourceBeg, sourceEnd)中每一个元素前趋差,然后写destBeg
//a1, a2-a1, a3-a2, a4-a3, ...
OutputIterator
adjacent_difference (InputIterator sourceBeg,
InputIterator sourceEnd,
OutputIterator destBeg)

//a1, a2 op a1, a3 op a2, a4 op a3, ...
OutputIterator
adjacent_difference (InputIterator sourceBeg,
InputIterator sourceEnd,
OutputIterator destBeg,
BinaryFunc op)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: