本节描述的算法会变动区间内的元素内容。有两种方法可以变动元素内容:

1.运用迭代器遍历序列的过程中,直接加以变动

2.将元素从源区间赋值到目标区间的过程中加以变动

复制(copy)元素

OutputIterator

copy(InputIterator sourceBeg,

InputIterator sourceEnd,

OutputIterator destBeg)

BiderectionalIterator

copy_backward(BidirectionalIterator sourceBeg,

BidirectionalIterator sourceEnd,

BidirectionalIterator destEnd)

1.这两个算法都将源区间[sourceBeg,sourceEnd)中的所有元素赋值到以destBeg为起点或以destEnd为终点的目标区间去

2.返回目标区间内最后一个被赋值元素的下一位置,也就是第一个违背覆盖的元素的位置

3.destBeg或destEnd不可处于[sourceBeg,sourceEnd)区间内

下面的例子展示copy()的一些简单用法

 #include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll1;
list<int> coll2;
INSERT_ELEMENTS(coll1,,);
copy(coll1.begin(),coll1.end(),back_inserter(coll2));
copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
cout<<endl;
copy(coll1.rbegin(),coll1.rend(),coll2.begin());
copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}

转换(Transforming)和结合(Combining)元素

算法transform()提供一下两种能力:

1.转换元素

OutputIterator

transform(InputIterator sourceBeg,InputIterator sourceEnd,

OutputIterator destBeg,UnaryFunc op)

针对源区间[sourceBeg,sourceEnd)中的每一个元素调用 op(elem),并将结果写到以destBeg起始的目标区间内。

下面这个例子展示了transform()的转换元素功能

 #include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll1;
list<int> coll2;
INSERT_ELEMENTS(coll1,,);
PRINT_ELEMENTS(coll1,"coll1: ");
transform(coll1.begin(),coll1.end(),coll1.begin(),negate<int>());
PRINT_ELEMENTS(coll1,"negated: ");
transform(coll1.begin(),coll1.end(),back_inserter(coll2),bind2nd(multiplies<int>(),));
PRINT_ELEMENTS(coll2,"coll2: ");
transform(coll2.rbegin(),coll2.rend(),ostream_iterator<int>(cout," "),negate<int>());
cout<<endl;
}

2.将两序列的元素加以结合

OutputIterator

transform(InputIterator source1Beg,InputIterator source1End,

InputIterator source2Beg,

OutputIterator destBeg,

BinaryFunc op)

针对第一源区间[source1Beg,source1End)以及“从source2Beg开始的第二个源区间“对应的元素,调用:

op(source1Elem,source2Elem) 并将结果以destBeg起始的目标区间内。

下面这个例子展示以上所说的transform()用法:

 #include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll1;
list<int> coll2;
INSERT_ELEMENTS(coll1,,);
PRINT_ELEMENTS(coll1,"coll1: ");
transform(coll1.begin(),coll1.end(),coll1.begin(),coll1.begin(),multiplies<int>());
PRINT_ELEMENTS(coll1,"squared: ");
transform(coll1.begin(),coll1.end(),coll1.rbegin(),back_inserter(coll2),plus<int>());
PRINT_ELEMENTS(coll2,"coll2: ");
cout<<"diff: ";
transform(coll1.begin(),coll1.end(),coll2.begin(),ostream_iterator<int>(cout," "),minus<int>());
cout<<endl;
}

互换(Swapping)元素内容

ForwardIterator

swap_ranges(ForwardIterator beg1,ForwardIterator end1,

ForwardIterator beg2)

1.强区间[beg1,end1)内的元素和”从beg2开始的区间“内的对应元素互换

2.返回第二区间中”最后一个被交换元素“的下一位置

下面这个例子展示swap_ranges()的用法

 #include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll1;
deque<int> coll2;
INSERT_ELEMENTS(coll1,,);
INSERT_ELEMENTS(coll2,,);
PRINT_ELEMENTS(coll1,"coll1: ");
PRINT_ELEMENTS(coll2,"coll2: ");
deque<int>::iterator pos;
pos=swap_ranges(coll1.begin(),coll1.end(),coll2.begin());
PRINT_ELEMENTS(coll1,"\ncoll1: ");
PRINT_ELEMENTS(coll2,"coll2: ");
if(pos!=coll2.end())
cout<<"first element not modified: "<<*pos<<endl;
swap_ranges(coll2.begin(),coll2.begin()+,coll2.rbegin());
PRINT_ELEMENTS(coll2,"\ncoll2:");
}

赋予(Assigning)新值

1.赋予完全相同的数值

void

fill(ForwardIterator beg,ForwardIterator end,

const T& newValue)

void

fill(ForwardIterator beg,Size num,

const T& newValue)

1.fill()将区间[beg,end)内的每一个元素都赋予新值newValue

2.fill_n()将”从beg开始的前num个元素“赋予新值newValue

3.调用者必须确保目标区间有足够空间,要不就得用插入行迭代器

以下程序展示fill()和fill_n()的用法

 #include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
fill_n(ostream_iterator<float>(cout," "),,7.7);
cout<<endl;
list<string> coll;
fill_n(back_inserter(coll),,"hello");
PRINT_ELEMENTS(coll,"coll: ");
fill(coll.begin(),coll.end(),"again");
PRINT_ELEMENTS(coll,"coll: ");
fill_n(coll.begin(),coll.size()-,"hi");
PRINT_ELEMENTS(coll,"coll: ");
list<string>::iterator pos1,pos2;
pos1=coll.begin();
pos2=coll.end();
fill(++pos1,--pos2,"hmmm");
PRINT_ELEMENTS(coll,"coll: ");
}

2.赋予新产生的数值

void

generate(ForwardIterator beg,ForwardIterator end,

Func op)

void

generate_n(OutputIterator beg,Size num,

Func op)

1.generate()会调用以下动作:op() ,并赋值给区间[beg,end)内的每个元素

2.generate_n()会调用以下动作: op(),并赋值给”以beg起始的区间“内的前num个元素

以下程序展示如何利用generate()和generatr_n()安插和赋值一些随机数

 #include <cstdlib>
#include "algostuff.hpp"
using namespace std; int main()
{
list<int> coll;
generate_n(back_inserter(coll),,rand);
PRINT_ELEMENTS(coll);
generate(coll.begin(),coll.end(),rand);
PRINT_ELEMENTS(coll);
}

替换(Replacing)元素

1.替换序列内的元素

void

replace(ForwardIterator beg,ForwardIterator end,

const T& oldValue,const T& newValue)

void

replace_if(ForwardIterator beg,ForwardIterator end,

UnaryPredicate op,const T& newValue)

1.replace()将区间[beg,end)之内每一个”与oldValue相等“的元素替换成newValue

2.replace_if()将区间[beg,end)之内每一个以下一元判断式:op(elem)返回true的元素替换成newValue

以下程序示范replace()和replace_if()的用法

 #include "algostuff.hpp"
using namespace std; int main()
{
list<int> coll;
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll,"coll: ");
replace(coll.begin(),coll.end(),,);
PRINT_ELEMENTS(coll,"coll: ");
replace_if(coll.begin(),coll.end(),bind2nd(less<int>(),),);
PRINT_ELEMENTS(coll,"coll: ");
}

2.复制并替换元素

OutputIterator

replace_copy(InputIterator sourceBeg,InputIterator sourceEnd,

OutputIterator destBeg,

const T& oldValud,const T& newValue)

OutputIterator

replace_copy_if(InputIterator sourceBeg,InputIterator sourceEnd,

OutputIterator destBeg,

UnaryPredicate op,const T& newValue)

1.replace_copy()是copy()和replace()的组合。他将源区间[beg,end)中的元素赋值到”以destBeg为起点“的目标区间

同时将其中”与oldValue相等“的所有元素替换为newValue

2.replace_copy()是copy()和replace_if()的组合。

以下程序示范如何使用replace_copy()和replace_copy_if()

 #include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
list<int> coll;
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll);
replace_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),,);
cout<<endl;
replace_copy_if(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),bind2nd(less<int>(),),);
cout<<endl;
}

STL学习笔记(变动性算法)的更多相关文章

  1. STL学习笔记(算法概述)

    算法头文件 要运用C++标准程序库的算法,首先必须包含头文件<algorithm> 使用STL算法时,经常需要用到仿函数以及函数配接器.它们定义域<functional>头文件 ...

  2. STL学习笔记--排序算法

    排序算法 C++ STL 的排序算法(Sorting algorithms)是一组将无序序列排列成有序序列的模板函数或与排序相关的模板函数,提供了排序.折半搜索.归并.集合操作.堆操作.最值求解.字典 ...

  3. STL学习笔记(五) 算法

    条款30:确保目标区间足够大 条款31:了解各种与排序有关的选择 //使用unaryPred划分输入序列,使得unaryPred为真的元素放在序列开头 partition(beg, end, unar ...

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

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

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

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

  6. Effective STL 学习笔记 39 ~ 41

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

  7. 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 ...

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

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

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

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

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

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

随机推荐

  1. c++ 操作防火墙

    原文转自 https://msdn.microsoft.com/zh-cn/library/aa364726(v=vs.85).aspx #include <windows.h> #inc ...

  2. 谈谈dpdk应用层包处理程序的多进程和多线程模型选择时的若干考虑

    看到知乎上有个关于linux多进程.多线程的讨论:http://www.zhihu.com/question/19903801/answer/14842584 自己项目里也对这个问题有过很多探讨和测试 ...

  3. 2017网易---Fibonacci数列

    题目描述 Fibonacci数列是这样定义的:F[0] = 0F[1] = 1for each i ≥ 2: F[i] = F[i-1] + F[i-2]因此,Fibonacci数列就形如:0, 1, ...

  4. python tips;matplotlib 显示中文

    import numpy as npimport matplotlib.pyplot as pltimport matplotlib as mpl mpl.rcParams['axes.unicode ...

  5. logrotate 学习使用

    logrotate Summary : Rotates, compresses, removes and mails system log files Description : The logrot ...

  6. MSSQL-字符串分离与列记录合并成一行混合使用

    一般我们在数据库的表字段存储字典Id,如果有多个的话一般是用,或分隔符分隔(12,14),列表显示的时候是显示字典名,那如果要在数据库将字典Id转成用户看得懂的字典名,该怎么办呢? 我们这时候可以结合 ...

  7. 2018 ICPC 徐州邀请赛 总结

    Day 0 上午在高铁里面,很困但是睡不着…… 中午到矿大报道. 食堂饭菜不错,有西瓜,就是有点辣. 下午热身赛,D题队友想了个假算法……GG. 评测机摸底考试正常进行. 热身赛之后精疲力尽,赶到宾馆 ...

  8. AppScan 浏览器兼容解决

    手动探索的时候,因为打开的浏览器是appscan自带的,可能会存在兼容性问题,有些页面无法正常打开.那么是否可以用我们电脑上的浏览器(IE .火狐.谷歌)来进行录制 菜单栏--工具---选项----首 ...

  9. 浅析 Node.js 单线程模型

    总结笔记:对于每个用户请求,由主线程接收并存放于一个事件队列中(不做任何处理),当无请求发生时,即主线程空闲,主线程开始循环处理事件队列中的任务: 对于非阻塞JS程序: 1.若某事件需要I/O操作,则 ...

  10. [LOJ6277]数列分块入门 1

    题目大意: 给你一个长度为$n(n\leq 50000)$的序列$A$,支持进行以下两种操作: 1.将区间$[l,r]$中所有数加上$c$: 2.询问$A_r$的值.思路: 分块. 对于整块的数据打标 ...