STL学习笔记(数值算法)
运用数值算法之前必须先加入头文件<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学习笔记(数值算法)的更多相关文章
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- 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 ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 30: 保证目标区间足够大
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
- 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 ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- Effective STL 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
- Effective STL 学习笔记:19 ~ 20
Effective STL 学习笔记:19 ~ 20 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
随机推荐
- C++11 lambda表达式(19篇C++11文章)
C++11引入了lambda表达式,使得程序员可以定义匿名函数,该函数是一次性执行的,既方便了编程,又能防止别人的访问. Lambda表达式的语法通过下图来介绍: 这里假设我们定义了一个如上图的lam ...
- ./configure时候遇到的问题 Cannot find install-sh, install.sh, or shtool in ac-aux
https://blog.csdn.net/anloan/article/details/17268997
- web前端工作五年了,我来告诉你如何系统的学习现在的JavaScript
一.入门 1:熟悉DIV+CSS布局 使用DIV+CSS布局标准网页,可以使前端XHTML代码更少.结构更清晰,这有利于轻松用JavaScript操作DOM 比如,要展示一个3行3列的列表,如果用传统 ...
- 解析转换json xml 集合 ado
json提取 string str = "[{\"JUDGE_RESULT\":\"B类\",\"JUDGE_RESULT\":\ ...
- Codeforces Round #445 C. Petya and Catacombs【思维/题意】
C. Petya and Catacombs time limit per test 1 second memory limit per test 256 megabytes input standa ...
- C++之++运算符重载问题
记录++之前先记一下左右值和存取数据的问题 数据的存放分三个部分,堆区,栈区和静态变量区 左值可以更改,右值不能更改 栈区和堆区存储的都是左值,可以随意更改其值,静态变量区部分数据是右值,比如cons ...
- Tarjan缩点+LCA【p2783】有机化学之神偶尔会做作弊
Description 你翻到那一题:给定一个烃,只含有单键(给初中生的一个理解性解释:就是一堆碳用横线连起来,横线都是单条的). 然后炎魔之王拉格纳罗斯用他的火焰净化了一切环(???).所有的环状碳 ...
- App Class Loader
Java本身是一种设计的非常简单,非常精巧的语言,所以Java背后的原理也很简单,归结起来就是两点: 1.JVM的内存管理 理解了这一点,所有和对象相关的问题统统都能解决 2.JVM Class Lo ...
- 【bzoj1123】【[POI2008]BLO】tarjan判割点
(上不了p站我要死了,侵权度娘背锅) Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有t ...
- Listener监听器笔记1
1.常用的Web事件监听器接口: 1.ServletContextListener:用于监听Web应用的启动和关闭. 2.ServletContextAttributeListener:用于监听Ser ...