跳到主要内容

std::rotate() 算法

// (1)
template< class ForwardIt >
constexpr ForwardIt rotate( ForwardIt first, ForwardIt middle, ForwardIt last );

// (2)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt rotate( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt middle, ForwardIt last );
  • (1) 对一系列元素执行左旋操作。

    具体来说,以这样一种方式交换范围 [first; last) 中的元素:将 [first; middle) 中的元素放在 [middle; last) 中的元素之后,同时保持两个范围中元素的顺序。

  • (2)(1) 相同,但根据 policy 执行。

    重载决议

    这些重载仅当 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (直到 C++20) std::is_execution_policy_v<std::rotate_cvref_t<ExecutionPolicy>>  (自 C++20 起)true 时才参与重载决议。

未定义行为

如果 [first; middle) 或 [middle; last) 不是有效范围,则行为未定义。

参数

first

原始范围的起始。

middle

指向应出现在旋转后范围开头的元素的迭代器。

last

原始范围的结束。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

类型要求

ForwardItLegacyForwardIterator
ValueSwappable
*first
*middle
last
MoveAssignable
MoveConstructible

返回值

一个迭代器,它等于

  • last,如果 first == middle
  • first,如果 middle == last
  • first + (last - middle),否则(first指向的元素的新位置)
    (不要求支持 + 和 - 操作,它们仅用于表示返回迭代器的位置)。

复杂度

firstlast 之间的距离成线性关系。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行时抛出异常,并且 ExecutionPolicy标准策略之一,则调用std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

可能的实现

rotate (1)
template<class ForwardIt>
constexpr // since C++20
ForwardIt rotate(ForwardIt first, ForwardIt middle, ForwardIt last)
{
if (first == middle)
return last;

if (middle == last)
return first;

ForwardIt write = first;
ForwardIt next_read = first; // read position for when "read" hits "last"

for (ForwardIt read = middle; read != last; ++write, ++read)
{
if (write == next_read)
next_read = read; // track where "first" went
std::iter_swap(write, read);
}

// rotate the remaining sequence into place
rotate(write, next_read, last);
return write;
}

备注

std::rotate 在常见实现上具有更好的效率,如果 ForwardIt 满足LegacyBidirectionalIterator 或(更好)LegacyRandomAccessIterator

当迭代器类型满足LegacyContiguousIterator,并且交换其值类型既不调用非平凡特殊成员函数也不调用ADL找到的交换函数时,实现(例如 MSVC STL)可能会启用向量化。

示例

std::rotate 是许多算法中常见的构建块。此示例演示插入排序。

Main.cpp
#include <algorithm>
#include <iostream>
#include <vector>

auto print = [](auto const remark, auto const& v)
{
std::cout << remark;
for (auto n : v)
std::cout << n << ' ';
std::cout << '\n';
};

int main()
{
std::vector<int> v {2, 4, 2, 0, 5, 10, 7, 3, 7, 1};
print("before sort:\t\t", v);

// insertion sort
for (auto i = v.begin(); i != v.end(); ++i)
std::rotate(std::upper_bound(v.begin(), i, *i), i, i + 1);
print("after sort:\t\t", v);

// simple rotation to the left
std::rotate(v.begin(), v.begin() + 1, v.end());
print("simple rotate left:\t", v);

// simple rotation to the right
std::rotate(v.rbegin(), v.rbegin() + 1, v.rend());
print("simple rotate right:\t", v);
}
输出
before sort:		2 4 2 0 5 10 7 3 7 1
after sort: 0 1 2 2 3 4 5 7 7 10
simple rotate left: 1 2 2 3 4 5 7 7 10 0
simple rotate right: 0 1 2 2 3 4 5 7 7 10
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::rotate() 算法

// (1)
template< class ForwardIt >
constexpr ForwardIt rotate( ForwardIt first, ForwardIt middle, ForwardIt last );

// (2)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt rotate( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt middle, ForwardIt last );
  • (1) 对一系列元素执行左旋操作。

    具体来说,以这样一种方式交换范围 [first; last) 中的元素:将 [first; middle) 中的元素放在 [middle; last) 中的元素之后,同时保持两个范围中元素的顺序。

  • (2)(1) 相同,但根据 policy 执行。

    重载决议

    这些重载仅当 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (直到 C++20) std::is_execution_policy_v<std::rotate_cvref_t<ExecutionPolicy>>  (自 C++20 起)true 时才参与重载决议。

未定义行为

如果 [first; middle) 或 [middle; last) 不是有效范围,则行为未定义。

参数

first

原始范围的起始。

middle

指向应出现在旋转后范围开头的元素的迭代器。

last

原始范围的结束。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

类型要求

ForwardItLegacyForwardIterator
ValueSwappable
*first
*middle
last
MoveAssignable
MoveConstructible

返回值

一个迭代器,它等于

  • last,如果 first == middle
  • first,如果 middle == last
  • first + (last - middle),否则(first指向的元素的新位置)
    (不要求支持 + 和 - 操作,它们仅用于表示返回迭代器的位置)。

复杂度

firstlast 之间的距离成线性关系。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行时抛出异常,并且 ExecutionPolicy标准策略之一,则调用std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

可能的实现

rotate (1)
template<class ForwardIt>
constexpr // since C++20
ForwardIt rotate(ForwardIt first, ForwardIt middle, ForwardIt last)
{
if (first == middle)
return last;

if (middle == last)
return first;

ForwardIt write = first;
ForwardIt next_read = first; // read position for when "read" hits "last"

for (ForwardIt read = middle; read != last; ++write, ++read)
{
if (write == next_read)
next_read = read; // track where "first" went
std::iter_swap(write, read);
}

// rotate the remaining sequence into place
rotate(write, next_read, last);
return write;
}

备注

std::rotate 在常见实现上具有更好的效率,如果 ForwardIt 满足LegacyBidirectionalIterator 或(更好)LegacyRandomAccessIterator

当迭代器类型满足LegacyContiguousIterator,并且交换其值类型既不调用非平凡特殊成员函数也不调用ADL找到的交换函数时,实现(例如 MSVC STL)可能会启用向量化。

示例

std::rotate 是许多算法中常见的构建块。此示例演示插入排序。

Main.cpp
#include <algorithm>
#include <iostream>
#include <vector>

auto print = [](auto const remark, auto const& v)
{
std::cout << remark;
for (auto n : v)
std::cout << n << ' ';
std::cout << '\n';
};

int main()
{
std::vector<int> v {2, 4, 2, 0, 5, 10, 7, 3, 7, 1};
print("before sort:\t\t", v);

// insertion sort
for (auto i = v.begin(); i != v.end(); ++i)
std::rotate(std::upper_bound(v.begin(), i, *i), i, i + 1);
print("after sort:\t\t", v);

// simple rotation to the left
std::rotate(v.begin(), v.begin() + 1, v.end());
print("simple rotate left:\t", v);

// simple rotation to the right
std::rotate(v.rbegin(), v.rbegin() + 1, v.rend());
print("simple rotate right:\t", v);
}
输出
before sort:		2 4 2 0 5 10 7 3 7 1
after sort: 0 1 2 2 3 4 5 7 7 10
simple rotate left: 1 2 2 3 4 5 7 7 10 0
simple rotate right: 0 1 2 2 3 4 5 7 7 10
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。