运用数值算法之前必须先加入头文件<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. 【原创】Linux环境下的图形系统和AMD R600显卡编程(5)——AMD显卡显命令处理机制

    通常通过读写设备寄存器对设备进行编程,在X86系统上,有专门的IO指令进行编程,在其他诸如MIPS.SPARC这类系统上,通过将设备的寄存器映射到内存地址空间直接使用读写内存的方式对设备进行编程. R ...

  2. VS MFC 添加菜单

    新建出来的基于对话框的MFC工程是没有菜单的,如何在对话框中添加菜单?又如何给菜单的菜单项添加事件应用响应?下面小编来具体描述一下,希望能帮助到一些人. 工具/原料   电脑一台 VS2010 方法/ ...

  3. UVA 11045 My T-shirt suits me

    一开始就想到网络流..后来一想暴力能不能过.自己写的T了.看了别人有暴力过的. 暴力的思路就是6进制数字表示给予的衣服的数量.然后每个人的需求表示成01 的6位串然后爆搜. 网络流就建一个源一个汇 然 ...

  4. python 的requests如何使用代理

    headers.py import random first_num = random.randint(55, 62) third_num = random.randint(0, 3200) four ...

  5. python 向mysql插入数据

    生成随机内容用到的方法: substr是一个字符串函数,从第二个参数1,开始取字符,取到3 + floor(rand() * 75)结束 floor函数代表的是去尾法取整数. rand()函数代表的是 ...

  6. cl编译C文件的环境变量修改

    添 加环境 变量INCLUDEC:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt;C:\Program Files (x8 ...

  7. Codeforces Beta Round #4 (Div. 2 Only) A. Watermelon【暴力/数学/只有偶数才能分解为两个偶数】

    time limit per test 1 second memory limit per test 64 megabytes input standard input output standard ...

  8. x-pack-crack

    破解x-pack-----------1. 编辑文件:LicenseVerifier.javapackage org.elasticsearch.license;import java.nio.*;i ...

  9. 将本地jar包安装进入maven仓库

    实际项目中pom.xml依赖写法: <dependency> <groupId>org.springframework</groupId> <artifact ...

  10. iOS UI、Xcode、调试、代码等常见问题总汇(持续更新中)

    以前比较懒,遇到问题解决了就完事了,有些问题再次遇到时忘记了当初是怎么解决的,又要查各种资料来解决.好记忆不如烂笔头,不管简单还是复杂都记一下吧,所以决定写一篇常见问题总结,方便以后查阅.现在有点忙, ...