STL学习笔记(数值算法)

运用数值算法之前必须先加入头文件<numeric>

加工运算后产生结果

1.对序列进行某种运算

T

accumulate(InputIterator beg,InputIterator end,

                   T initValue)

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 }
View Code

2.计算两序列的内积

T

inner_product(InputIterator beg1,InputIterator end1,

                       InputIterator beg2,T initValue)

T

inner_product(InputIterator beg1,InputIterator end1,

                       InputIterator beg2,T initValue,

                       BinaryFunc op1,BinaryFunc op2)

1.第一种形式针对“两区间内的每一组对应元素”调用以下表达式:

   initValue=initValue+elem1+elem2

2.第二形式则调用以下表达式:

  initValue=op1(initValue,op2(elem1,elem2))

以下程序示范inner_product()的用法

 1 #include "algostuff.hpp"
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     list<int> coll;
 7     INSERT_ELEMENTS(coll,1,6);
 8     PRINT_ELEMENTS(coll);
 9     cout<<"inner product: "
10         <<inner_product(coll.begin(),coll.end(),coll.begin(),0)
11         <<endl;
12     cout<<"inner reverse product: "
13         <<inner_product(coll.begin(),coll.end(),coll.rbegin(),0)
14         <<endl;
15     cout<<"product of sums: "
16         <<inner_product(coll.begin(),coll.end(),coll.begin(),1,multiplies<int>(),plus<int>())
17         <<endl;
18 }
View Code

相对值跟绝对值之间的转换

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

OutputIterator

partial_sum(InputIterator sourceBeg,

                    InputIterator sourceEnd,

                    OutputIterator destBeg)

OutputIterator

partial_sum(InputIterator sourceBeg,

                    InputIterator sourceEnd,

                    OutputIterator destBeg,BinaryFunc op)

1.第一形式计算源区间[sourceBeg,sourceEnd)中每个元素的部分和,然后将结果写入以destBeg为起点的目标区间

2.第二形式将源区间[sourceBeg,sourceEnd)中的每个元素和其先前所有元素进行op运算,并将结果写入destBeg为起点的目标区间

   例如对于以下数值序列:a1 a2 a3 ...

   它们分别计算:

   a1,a1+a2,a1+a2+a3,..

   a1,a1 op a2,a1 op a2 op a2,...

以下程序示范partial_sum()的用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     vector<int> coll;
 8     INSERT_ELEMENTS(coll,1,6);
 9     PRINT_ELEMENTS(coll);
10     partial_sum(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
11     cout<<endl;
12     partial_sum(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),multiplies<int>());
13     cout<<endl;
14 }
View Code

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

OutputIterator

adjacent_difference(InputIterator sourceBeg,

                                InputIterator sourceEnd,

                                OutputIterator destBeg)

OutputIterator

adjacent_difference(InputIterator sourceBeg,

                                InputIterator sourceEnd,

                                OutputIterator destBeg)

1.第一种形式计算区间[sourceBeg,sourceEnd)中每一个元素和其紧邻前驱元素的差额,并将结果写入destBeg为起点的目标区间

2.第二种形式针对区间[sourceBeg,sourceEnd)中每一个元素和其紧邻前驱元素调用op操作,并将结果写入destBeg为起点的目标区间

   对于以下数值序列:

   a1,a2,a3,a4,...

   它们分别计算:

   a1,a2-a1,a3-a2,a4-a3,...

   a1,a2 op a1,a3 op a2,a4 op a3,...

以下程序示范adjacent_difference()的用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     deque<int> coll;
 8     INSERT_ELEMENTS(coll,1,6);
 9     PRINT_ELEMENTS(coll);
10     adjacent_difference(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),plus<int>());
11     cout<<endl;
12     adjacent_difference(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),multiplies<int>());
13     cout<<endl;
14 }
View Code
原文地址:https://www.cnblogs.com/runnyu/p/4849852.html