一. 概述
变序性算法改变元素次序,但不改变元素值。这些算法不能用于关联式容器,因为关联式容器中,元素有一定的次序。
| reverse() | 将元素的次序逆转 |
| reverse_copy() | 复制的同时,逆转元素顺序 |
| rotate() | 旋转元素次序 |
| rotate_copy() | 复制的同时,旋转元素顺序 |
| next_permutation() | 得到元素的下一个排列次序 |
| prev_permutation() | 得到元素的上一个排列次序 |
| random_shuffle() | 将元素的次序次序随机打乱 |
| partition() | 改变元素次序,使符合某准则者移到前面 |
| stable_partition() | 与partition()相似,但保持符合准则与不符合准则各个元素之间的相对位置 |
//将区间[beg, end)内的元素全部逆序
void
reverse (BidirectionalIterator beg, BidirectionalIterator end)
//将源区间[beg, end)内元素复制到以destBeg起始目标区间,
//并在复制过程中逆序
OutputIterator
reverse_copy (BidirectionalIterator sourceBeg, BidirectionalIterator
sourceEnd,
Output Iterator destBeg)三. 旋转元素次序
1. 旋转序列内的元素
//将区间[beg, end)内的元素进行旋转,执行后*newBeg成为新的第一元素
//调用者必须确保newBeg是区间[beg, end)内的一个有效位置
void
rotate (ForwardIterator beg,
ForwardIterator newBeg,
ForwardIterator end)2. 复制并同时旋转元素//将源区间[sourceBeg, sourceEnd)内的元素复制到以destBeg起始的目标区间中,
//同时旋转元素,使newBeg成为新的第一个元素
OutputIterator
rotate_copy (ForwardIterator sourceBeg,
ForwardIterator newBeg,
ForwardIterator sourceEnd,
OutputIterator destBeg)四. 排列元素bool
next_permutation (BidirectionalIterator beg, BidirectionalIterator end)
bool
prev_permutation (BidirectionalIterator beg, BidirectionalIterator end)五. 随机排序
//使一个均匀分布随机数来打乱区间[beg, end)内的元素
void
random_shuffle (RandomAccessIterator beg, RandomAccessIterator end)
//使用op打乱区间[beg, end)内的元素次序
void
random_shuffle (RandomAccessIterator beg, RandomAccessIterator end,
RandomFunc& op)六. 将元素向前搬移
//将区间[beg, end)中使op(elem)为true的元素向前移动
BidirectionalIterator
partition (BidirectionalIterator beg, BidirectionalIterator end,
UnaryPredicate op)
//会保持它们之前的相对次序
BidirectionalIterator
stable_partition (BidirectionalIterator beg, BidirectionalIterator end,
UnaryPredicate op)

本文介绍了变序性算法的基本概念及其应用场景,包括逆转、旋转、排列和随机排序等操作。详细阐述了reverse、reverse_copy、rotate、rotate_copy、next_permutation、prev_permutation、random_shuffle、partition和stable_partition等算法的工作原理。
557

被折叠的 条评论
为什么被折叠?



