STL学习笔记(变序性算法)

变序性算法改变元素的次序,但不改变元素值。

这些算法不能用于关联式容器,因为在关联式容器中,元素有一定的次序,不能随意变动。

逆转元素次序

void

reverse(BidirectionalIterator beg,BidirectionalIterator end)

OutputIterator

reverse_copy(BidirectionalIterator sourceBeg,BidirectionalIterator sourceEnd,

OutputIterator destBeg)

1.reverce()会将区间[beg,end)内的元素全部逆序

2.reverse_copy()是reverse()跟copy()的组合

下面这个程序展示reverse()和reverse_copy()的用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4
 5 int main()
 6 {
 7     vector<int> coll;
 8     INSERT_ELEMENTS(coll,1,9);
 9     PRINT_ELEMENTS(coll,"coll: ");
10     reverse(coll.begin(),coll.end());
11     PRINT_ELEMENTS(coll,"coll: ");
12     reverse(coll.begin()+1,coll.end()-1);
13     PRINT_ELEMENTS(coll,"coll: ");
14     reverse_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
15     cout<<endl;
16 }

旋转(Rotating)元素次序

1.旋转序列内的元素

void

rotate(ForwardIterator beg,ForwardIterator newBeg,

ForwardIterator end)

1.将区间[beg,end)内的元素进行旋转,执行后*newBeg成为新的第一元素

2.调用者必须确保newBeg是[beg,end)内的一个有效位置,否则会引发未定义的行为

以下程序示范如何使用rotate()

 1 #include "algostuff.hpp"
 2 using namespace std;
 3
 4 int main()
 5 {
 6     vector<int> coll;
 7     INSERT_ELEMENTS(coll,1,9);
 8     PRINT_ELEMENTS(coll,"coll: ");
 9     rotate(coll.begin(),coll.begin()+1,coll.end());
10     PRINT_ELEMENTS(coll,"one left: ");
11     rotate(coll.begin(),coll.end()-2,coll.end());
12     PRINT_ELEMENTS(coll,"two right: ");
13     rotate(coll.begin(),find(coll.begin(),coll.end(),4),coll.end());
14     PRINT_ELEMENTS(coll,"4 first: ");
15 }

2.复制并同时旋转元素

OutputIterator

rotate_copy(ForwardIterator sourceBeg,ForwardIterator newBeg,

ForwardIterator sourceEnd,

OutputIterator destBeg)

这是copy()和rotate()的组合

以下程序示范rotate_copy()的用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4
 5 int main()
 6 {
 7     set<int> coll;
 8     INSERT_ELEMENTS(coll,1,9);
 9     PRINT_ELEMENTS(coll);
10     set<int>::iterator pos=coll.begin();
11     advance(pos,1);
12     rotate_copy(coll.begin(),pos,coll.end(),ostream_iterator<int>(cout," "));
13     cout<<endl;
14     pos=coll.end();
15     advance(pos,-2);
16     rotate_copy(coll.begin(),pos,coll.end(),ostream_iterator<int>(cout," "));
17     cout<<endl;
18     rotate_copy(coll.begin(),coll.find(4),coll.end(),ostream_iterator<int>(cout," "));
19     cout<<endl;
20 }

排列元素(Permuting)元素

bool

next_permutation(BidirectionalIterator beg,

BidirectionalIterator end)

bool

prev_permutation(BidirectionalIterator beg,

BidirectionalIterator end)

1.next_permutation()会改变区间[beg,end)内的元素次序,使他们符合“下一个排列次序”

2.prev_permutation()会改变区间[beg,end)内的元素次序,是他们符合“上一个排列次序”

下面这个例子展示利用next_permutation()和prev_permutation()将所有元素的所有可能排列的过程

 1 #include "algostuff.hpp"
 2 using namespace std;
 3
 4 int main()
 5 {
 6     vector<int> coll;
 7     INSERT_ELEMENTS(coll,1,3);
 8     PRINT_ELEMENTS(coll,"on entry: ");
 9     while(next_permutation(coll.begin(),coll.end()))
10         PRINT_ELEMENTS(coll," ");
11     PRINT_ELEMENTS(coll,"afterward: ");
12     while(prev_permutation(coll.begin(),coll.end()))
13         PRINT_ELEMENTS(coll," ");
14     PRINT_ELEMENTS(coll,"now: ");
15     while(prev_permutation(coll.begin(),coll.end()))
16         PRINT_ELEMENTS(coll," ");
17     PRINT_ELEMENTS(coll,"afterward: ");
18 }

重排元素(Shuffling,搅乱次序)

void

random_shuffle(RandomAccessIterator beg,RandomAccessIterator end)

void

random_shuffle(RandomAccessIterator beg,RandomAccessIterator end,

RandomFunc& op)

1.第一形式使用一个均匀分布随机数产生器来打乱区间[beg,end)内的元素次序

2.第二形式使用op打乱区间[beg,end)内的元素次序。

以下程序示范如何调用random_shuffle()来打乱元素次序

 1 #include <cstdlib>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4
 5 class MyRandom
 6 {
 7 public:
 8     ptrdiff_t operator()(ptrdiff_t max)
 9     {
10         double tmp;
11         tmp=static_cast<double>(rand())/static_cast<double>(RAND_MAX);
12         return static_cast<ptrdiff_t>(tmp * max);
13     }
14 };
15
16 int main()
17 {
18     vector<int> coll;
19     INSERT_ELEMENTS(coll,1,9);
20     PRINT_ELEMENTS(coll,"coll: ");
21     random_shuffle(coll.begin(),coll.end());
22     PRINT_ELEMENTS(coll,"shuffled: ");
23     sort(coll.begin(),coll.end());
24     PRINT_ELEMENTS(coll,"sorted: ");
25     MyRandom rd;
26     random_shuffle(coll.begin(),coll.end(),rd);
27     PRINT_ELEMENTS(coll,"shuffled: ");
28 }

将元素向前搬移

BidirectionalIterator

partition(BidirectionalIterator beg,

BidirectionalIterator end,

UnaryPredicate op)

BidirectionalIterator

stable_partition(BidirectionalIterator beg,

BidirectionalIterator end,

UnaryPredicate op)

1.这两种算法将区间[beg,end)中造成以下一元判断式:op(elem)结果为true的元素向前端移动

2.stable_partition()会保持元素之间的相对次序。

以下程序示范partition()和stable_partition()的用法以及两者的区别

 1 #include "algostuff.hpp"
 2 using namespace std;
 3
 4 int main()
 5 {
 6     vector<int> coll1;
 7     vector<int> coll2;
 8     INSERT_ELEMENTS(coll1,1,9);
 9     INSERT_ELEMENTS(coll2,1,9);
10     PRINT_ELEMENTS(coll1,"coll1: ");
11     PRINT_ELEMENTS(coll2,"coll2: ");
12     cout<<endl;
13     vector<int>::iterator pos1,pos2;
14     pos1=partition(coll1.begin(),coll1.end(),not1(bind2nd(modulus<int>(),2)));
15     pos2=stable_partition(coll2.begin(),coll2.end(),not1(bind2nd(modulus<int>(),2)));
16     PRINT_ELEMENTS(coll1,"coll1: ");
17     cout<<"first odd element: "<<*pos1<<endl;
18     PRINT_ELEMENTS(coll2,"coll1: ");
19     cout<<"first odd elements: "<<*pos2<<endl;
20     PRINT_ELEMENTS(coll2,"coll2: ");
21 }

时间: 2024-08-28 16:36:43

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

STL学习笔记(非变动性算法)

辅助函数 本节跟以后几节将对所有STL算法逐一详细讨论.为了简化这些例子,我们使用了一些辅助函数,分别用于对容器进行输出跟插入操作. for_each()算法 for_each()算法非常灵活,它可以以不同的方式存取.处理.修改每一个元素 UnaryProc for_each(InputIterator beg,InputIterator end,UnaryProc op); 1.对与区间[beg,end)中的每一个元素调用:op(elem) 2.返回op(已在算法内部被变动过)的一个副本 3.

STL学习笔记(算法概述)

算法头文件 要运用C++标准程序库的算法,首先必须包含头文件<algorithm> 使用STL算法时,经常需要用到仿函数以及函数配接器.它们定义域<functional>头文件中. 算法的分类 可以按以下分类方式描述各个STL算法: 非变动性算法(nonmodifying algorithms) 变动性算法(modifying algorithms) 移除性算法(removing algorithms) 变序性算法(mutating algorithms) 排序算法(sorting

Linux系统学习笔记:序

Linux系统学习笔记:序 ??Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协议.它支持32位和64位硬件.Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统. 本人使用的Linux为Ubuntu,主要以<APUE>(第3版)为学习蓝本. 1. Unix/Linux 体系结构 如图: 内核的接口被称为系统调用.公用函数库构建在

STL学习笔记--&gt;初识STL

“这里要用char类型”; “这里要用int类型”; “其实实现这个方法只需要把另一个方法的返回值的类型和传入参数的类型改成float类型就实现了”; “其实这个算法只需要把以前写的那个稍微改动一下就行了”; ……………… 学过面向对象语言的都知道GP这个概念,就是泛型程序设计,说的再明白点就是编写不依赖于具体数据类型的程序,C++作为一门面向对象语言,当然也有泛型这个概念,这就不得不提STL(Standard Template Library,标准模板库),是被融入C++标准程序库里面的一个高

STL学习笔记--3、迭代器iterator与traits编程

iterator模式:提供一种方法,依次巡访某个聚合物(容器)所含的各个元素,而无需暴露该聚合物的内部表达式. 1.迭代器设计思维 STL在于将数据容器和算法分开,彼此独立,最后再以一帖粘合剂将它们撮合在一起.只要对算法给予不同的迭代器,就可以对不同容器进行相同的操作. 算法find():接受两个迭代器和一个搜寻目标. //摘自SGI<stl_algo.h> template <class InputIterator, class T> InputIterator find(Inp

【视频编解码&#183;学习笔记】8. 熵编码算法:基本算法列举 &amp; 指数哥伦布编码

一.H.264中的熵编码基本方法: 熵编码具有消除数据之间统计冗余的功能,在编码端作为最后一道工序,将语法元素写入输出码流 熵解码作为解码过程的第一步,将码流解析出语法元素供后续步骤重建图像使用 在H.264的标准协议中,不同的语法元素指定了不同的熵编码方法.在协议文档中共指定了10种语法元素的描述符,这些描述符表达了码流解析为语法元素值的方法,其中包含了H.264标准所支持的所有熵编码方法: 语法元素描述符 编码方法 b(8) 8位二进制比特位串,用于描述rbsp_byte() f(n) n位

STL学习笔记(已序区间算法)

针对已序区间执行的算法,执行前提是源区间必须在某个排序准则下已序. 搜寻元素(Searching) 1.检查某个元素是否存在 bool binary_search(ForwardIterator beg,ForwardIterator end, const T& value) bool binary_search(ForwardIterator beg,ForwardIterator end, const T& value, BinaryPredicate op) 以下示范binary_s

STL学习笔记(变动性算法)

本节描述的算法会变动区间内的元素内容.有两种方法可以变动元素内容: 1.运用迭代器遍历序列的过程中,直接加以变动 2.将元素从源区间赋值到目标区间的过程中加以变动 复制(copy)元素 OutputIterator copy(InputIterator sourceBeg, InputIterator sourceEnd, OutputIterator destBeg) BiderectionalIterator copy_backward(BidirectionalIterator sourc

算法导论学习笔记1---排序算法(平台:gcc 4.6.7)

平台:Ubuntu 12.04/gcc 4.6.7 插入排序 1 #include<vector> 2 #include <algorithm> 3 #include<iostream> 4 using namespace std; 5 6 template <typename T> 7 void insertSort(vector<T>& vec){ 8 //vector<T>::iterator ite; 9 for(au