运用数值算法之前必须先加入头文件<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. MFC 获得各类指针、句柄的方法(转)

    原文转自 https://blog.csdn.net/abcjennifer/article/details/7480019 1.MFC中获取常见类句柄<视图类,文档类,框架类,应用程序类> ...

  2. VS2013 Sqlite3 操作指令

    extern "C"{ #include "sqlite3.h" }; #pragma comment(lib,"sqlite.lib") ...

  3. 列表控件ListBox关联的MFC中的类:CListBox

    列表控件ListBox关联的MFC中的类:CListBox ######################################################## 1.在列表的结尾添加一项: ...

  4. 关于JAVA SESSION的小测试

    手生就要多练啊... package com.jeelearning.servlet; import java.io.IOException; import java.io.PrintWriter; ...

  5. AC日记——Valued Keys codeforces 801B

    801B - Valued Keys 思路: 水题... 来,上代码: #include <cstdio> #include <cstring> #include <io ...

  6. react 使用antd的TreeSelect树选择组件实现多个树选择循环

    需求说明,一个帐号角色可以设置管理多个项目的菜单权限 且菜单接口每次只能查询特定项目的菜单数据[无法查全部] 开发思路: 1,获取项目接口数组,得到项目数据 2,循环项目数据,以此为参数递归查询菜单数 ...

  7. 在React组件unmounted之后setState的报错处理

    最近在做项目的时候遇到一个问题,在 react 组件 unmounted 之后 setState 会报错.我们先来看个例子, 重现一下问题: class Welcome extends Compone ...

  8. 用CSS3产生动画效果

    相关属性: @keyframes规则:定义动画 语法:@keyframes animationname{keyframes-selector {CSS-style;}} animationname:动 ...

  9. 玩转shell之符号篇

    转:http://hi.baidu.com/hellosimple/item/21b31dfefd23e811e2e3bd47 在shell中常用的特殊符号罗列如下: # ; ;; . , / \\ ...

  10. ASIHTTPRequest学习(四)

    如果是IOS5的版本,可能集成过程中会遇到一些问题,我也找到了一些解决方案,比如,集成完后可能会遇到编译提示找不到"libxml/HTMLparser.h",解决这个问题可以参考这 ...