算法库
算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last)
,其中 last
指代要查询或修改的最后元素的后一个元素。
迁移范围中的元素
std::shift_left,
std::shift_right
template< class ForwardIt > constexpr ForwardIt shift_left( ForwardIt first, ForwardIt last, typename std::iterator_traits<ForwardIt>::difference_type n ); | (1) | (C++20 起) |
template< class ExecutionPolicy, class ForwardIt > ForwardIt shift_left( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, typename std::iterator_traits<ForwardIt>::difference_type n ); | (2) | (C++20 起) |
template< class ForwardIt > constexpr ForwardIt shift_right( ForwardIt first, ForwardIt last, typename std::iterator_traits<ForwardIt>::difference_type n ); | (3) | (C++20 起) |
template< class ExecutionPolicy, class ForwardIt > ForwardIt shift_right( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, typename std::iterator_traits<ForwardIt>::difference_type n ); | (4) | (C++20 起) |
将范围 [first, last)
中的元素迁移 n
个位置。
1) 向范围开端迁移元素。若 n <= 0 || n >= last - first 则无效果。否则,对于每个 [0, last - first - n) 中的整数 i
,移动原于位置 first + n + i 的元素到位置 first + i 。以 i
从 0 开始递增的顺序进行移动。
3) 向范围结尾迁移元素。若 n <= 0 || n >= last - first 则无效果。否则对于每个 [0, last - first - n) 中的整数 i
,移动原于位置 first + i 的元素到位置 first + n + i 。若 ForwardIt
满足遗留双向迭代器 (LegacyBidirectionalIterator) 的要求,则以 i
从 last - first - n - 1 开始递减的顺序进行移动。
2,4) 分别同 (1) 与 (3) ,但按照 policy
执行并可能以任何顺序进行移动。这些重载仅若 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true 才参与重载决议。
在原范围但不在新范围中的元素被置于合法但未指定的状态。
参数
first | - | 原范围的开端 |
last | - | 原范围的结尾 |
n | - | 要迁移的位置数 |
policy | - | 所用的执行策略。细节见执行策略。 |
类型要求 | ||
- ForwardIt 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。 | ||
- 对于重载 (3-4) ForwardIt 必须满足遗留双向迭代器 (LegacyBidirectionalIterator) 的要求或值可交换 (ValueSwappable) 的要求。 | ||
- 解引用 ForwardIt 结果的类型必须满足可移动赋值 (MoveAssignable) 的要求。 |
返回值
1-2) 结果范围的结尾。若 n
为正且小于 last - first
,则返回 first + (last - first - n)
。否则若 n
为正,则返回 first
。否则返回 last
。
3-4) 结果范围的开始。若 n
为正且小于 last - first
,则返回 first + n
。否则若 n
为正,则返回 last
。否则返回 first
。
复杂度
1-2) 至多 std::distance(first, last) - n 次赋值。
3-4) 至多 std::distance(first, last) - n 次赋值或交换。
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 若作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
为标准策略之一,则调用 std::terminate 。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 若算法无法分配内存,则抛出 std::bad_alloc 。
调用示例
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <vector>
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator >(const Cell &cell) const
{
if (x == cell.x)
{
return y > cell.y;
}
else
{
return x > cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
namespace std
{
template< class ForwardIt >
constexpr ForwardIt shift_left(ForwardIt first, ForwardIt last,
typename std::iterator_traits<ForwardIt>::difference_type n)
{
if (n <= 0 || first == last)
{
return first;
}
ForwardIt mid = first + n;
if (mid >= last)
{
std::rotate(first, mid, last);
return first;
}
ForwardIt new_end = last - n;
std::rotate(first, mid, last);
std::copy(new_end, last, first);
return first;
}
template< class ForwardIt >
constexpr ForwardIt shift_right(ForwardIt first, ForwardIt last,
typename std::iterator_traits<ForwardIt>::difference_type n)
{
if (n <= 0 || first == last)
{
return first;
}
auto length = std::distance(first, last);
n %= length; // 防止 n 大于容器长度的情况
if (n == 0)
{
return first;
}
ForwardIt new_first = first + n;
std::rotate(first, new_first, last);
return new_first;
}
}
int main()
{
std::cout << std::boolalpha;
// std::mt19937 g{std::random_device{}()};
srand((unsigned)time(NULL));
auto generate = []()
{
int n = std::rand() % 10 + 110;
Cell cell{n, n};
return cell;
};
//3) 构造拥有 count 个有值 value 的元素的容器。
std::vector<Cell> vector1(6, generate());
std::generate(vector1.begin(), vector1.end(), generate);
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
//将范围 [first, last) 中的元素迁移 n 个位置。
//1) 向范围开端迁移元素。若 n <= 0 || n >= last - first 则无效果。否则,对于每个 [0, last - first - n) 中的整数 i ,
//移动原于位置 first + n + i 的元素到位置 first + i 。以 i 从 0 开始递增的顺序进行移动。
std::shift_left(vector1.begin(), vector1.end(), 3);
std::cout << "shift_left vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
//3) 构造拥有 count 个有值 value 的元素的容器。
std::vector<Cell> vector2(6, generate());
std::generate(vector2.begin(), vector2.end(), generate);
std::cout << "vector2: ";
std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
//3) 向范围结尾迁移元素。若 n <= 0 || n >= last - first 则无效果。否则对于每个 [0, last - first - n) 中的整数 i ,
//移动原于位置 first + i 的元素到位置 first + n + i 。
//若 ForwardIt 满足遗留双向迭代器 (LegacyBidirectionalIterator) 的要求,
//则以 i 从 last - first - n - 1 开始递减的顺序进行移动。
std::shift_right(vector2.begin(), vector2.end(), 3);
std::cout << "shift_right vector2: ";
std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
return 0;
}
输出
vector1: {116,116} {113,113} {116,116} {111,111} {113,113} {115,115}
shift_left vector1: {116,116} {113,113} {116,116} {116,116} {113,113} {116,116}
vector2: {110,110} {110,110} {112,112} {114,114} {117,117} {118,118}
shift_right vector2: {114,114} {117,117} {118,118} {110,110} {110,110} {112,112}