运用数值算法之前必须先加入头文件<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()得到区间内所有元素的总和和乘积:

 #include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll);
cout<<"sum: "
<<accumulate(coll.begin(),coll.end(),)
<<endl;
cout<<"sum: "
<<accumulate(coll.begin(),coll.end(),-)
<<endl;
cout<<"product: "
<<accumulate(coll.begin(),coll.end(),,multiplies<int>())
<<endl;
cout<<"product: "
<<accumulate(coll.begin(),coll.end(),,multiplies<int>())
<<endl;
}

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()的用法

 #include "algostuff.hpp"
using namespace std; int main()
{
list<int> coll;
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll);
cout<<"inner product: "
<<inner_product(coll.begin(),coll.end(),coll.begin(),)
<<endl;
cout<<"inner reverse product: "
<<inner_product(coll.begin(),coll.end(),coll.rbegin(),)
<<endl;
cout<<"product of sums: "
<<inner_product(coll.begin(),coll.end(),coll.begin(),,multiplies<int>(),plus<int>())
<<endl;
}

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

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()的用法

 #include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll);
partial_sum(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
cout<<endl;
partial_sum(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),multiplies<int>());
cout<<endl;
}

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()的用法

 #include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
deque<int> coll;
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll);
adjacent_difference(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),plus<int>());
cout<<endl;
adjacent_difference(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),multiplies<int>());
cout<<endl;
}

STL学习笔记(数值算法)的更多相关文章

  1. Effective STL 学习笔记 39 ~ 41

    Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  2. Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value

    Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...

  3. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  4. Effective STL 学习笔记 32 ~ 33

    Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  5. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  6. Effective STL 学习笔记 Item 30: 保证目标区间足够大

    Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...

  7. Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor

    Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...

  8. Effective STL 学习笔记: Item 22 ~ 24

    Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...

  9. Effective STL 学习笔记 Item 21:Comparison Function 相关

    Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...

  10. Effective STL 学习笔记:19 ~ 20

    Effective STL 学习笔记:19 ~ 20 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

随机推荐

  1. session和xsrf

    1.pip install pycket 2.pip install redis 防止xsrf攻击只需在模板form标签加入: {% module xsrf_form_html() %} <!D ...

  2. ubuntu启动脚本一览分析

    #rc--run command的意思[rc解释]harvey@ubuntu:/etc$ cat ./init/rc-sysinit.conf # rc-sysinit - System V init ...

  3. windows下xampp安装PHP的pthreads多线程扩展

    我的运行环境: 系统:windows10 ,64位 PHP:5.6.8 TS,VC11 ,32位 Apache: 2.0 我安装的是xampp集成环境 pthreads的windows扩展文件下载地址 ...

  4. maven 在 mac中的配置

    思前想后,还是在mac中把maven配置一下吧. 1.下载安装包,由于公司用的版本比较低,考虑到兼容性,建议用低版本的.我用3.0.5 下载地址:http://archive.apache.org/d ...

  5. AC日记——算术天才⑨与等差数列 bzoj 4373

    4373 思路: 判断一个数列是否是等差数列: 1,最大值减去最小值==(区间个数-1)*k: 2,gcd==k: 3,不能有重复(不会这判断这一条,但是数据水就过了): 来,上代码: #includ ...

  6. 打印控件Lodop的使用

    前些天发现一个不错的打印的控件Lodop,下面就来介绍一下具体使用! 首先到官网:http://www.lodop.net/download.html 下载最新版,文档的话官网中有很详细的介绍,这里演 ...

  7. UVALive(LA) 4487 Exclusive-OR(带权并查集)

    题意:对于n个数X[0]~X[n-1],但你不知道它们的值,通过逐步提供给你的信息,你的任务是根据这些信息回答问题,有三种信息如下: I p  v : Xp = v;    Xp 的值为v I p q ...

  8. [BZOJ 1037] 生日聚会Party

    Link: BZOJ 1037 传送门 Solution: 由于对任意一段都有要求,于是我们对于所有前缀考虑其后缀不超过$k $即可: 设$dp[i][j][x][y]$为前$i$个人中有$j$个男孩 ...

  9. Shader与AGAL(From 7yue)

  10. Linux下将MySQL服务添加到服务器的系统服务中

    Linux下将MySQL服务添加到服务器的系统服务中 Linux环境下将MySQL服务添加到服务器的系统服务中 1.了解MySQL程序路径 MySQL数据目录: /home/mysql/dataMyS ...