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; ...
随机推荐
- 新建module---获取带宽信息
借鉴自http://blog.csdn.net/xjtuse2014/article/details/53968726 1.MoniterBandwidth模块: package net.floodl ...
- c/c++类型转换相关总结
在c语言中存在两种类型转换:显式类型转换和隐式类型转换: 显示类型转换:在类型前加上(type)变量,对变量进行的转换,程序员自己显式添加: char *ptra = (char*)ptrb; voi ...
- 有关cookie的内容
包括: Cookie概述(Cookie的存放,有效期和作用域) Cookie操作(保存Cookie,读取Cookie,Cookie的生命周期) Cookie工作原理(Cookie与会话跟踪,Cooki ...
- oracle 批量改temp/data/redo file的路径
批量生成修改路径的脚本.select 'alter database rename file ''' || name ||'''' || ' to '''|| substr(name,0,instr( ...
- Selenium2+python自动化11-定位一组元素find_elements【转载】
前言 前面的几篇都是讲如何定位一个元素,有时候一个页面上有多个对象需要操作,如果一个个去定位的话,比较繁琐,这时候就可以定位一组对象. webdriver 提供了定位一组元素的方法,跟前面八种定位方式 ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)- 勤奋的杨老师(最长递增子序列)
链接:https://www.nowcoder.com/acm/contest/116/C来源:牛客网 题目描述 杨老师认为他的学习能力曲线是一个拱形.勤奋的他根据时间的先后顺序罗列了一个学习清单,共 ...
- oracle数据迁移之Exp和Expdp导出数据的性能对比与优化
https://wangbinbin0326.github.io/2017/03/31/oracle%E6%95%B0%E6%8D%AE%E8%BF%81%E7%A7%BB%E4%B9%8BExp%E ...
- 内连接(INNER JOIN)
内连接组合两张表,并且基于两张表中的关联关系来连接它们.使用内连接需要指定表中哪些字段组成关联关系,并且需要指定基于什么条件进行连接.内连接的语法如下: INNER JOIN table_name O ...
- UVA 11396 Claw Decomposition 染色
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- socket 和 webservice 的区别和比较
时间紧迫,我就直奔主题. 目前需要说服客户使用webservice 而不是socket. 我觉得要先分别解释下什么是socket 什么是webservice..这个要我该怎么说才比较形象,让人一定就明 ...