首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::rotate

Defined in header <algorithm>

(1)

template< class ForwardIt > void rotate( ForwardIt first, ForwardIt n_first, ForwardIt last );

(until C++11)

template< class ForwardIt > ForwardIt rotate( ForwardIt first, ForwardIt n_first, ForwardIt last );

(since C++11)

template< class ExecutionPolicy, class ForwardIt > ForwardIt rotate( ExecutionPolicy&& policy, ForwardIt first, ForwardIt n_first, ForwardIt last );

(2)

(since C++17)

1%29对一系列元素执行左旋转。

特别是,std::rotate交换范围内的元素[first, last)以这样的方式使元素n_first成为新范围的第一个元素,并且n_first - 1成为最后一个元素。

这个函数的一个先决条件是[first, n_first)[n_first, last)是有效范围。

2%29与%281%29相同,但根据policy此重载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first

-

the beginning of the original range

n_first

-

the element that should appear at the beginning of the rotated range

last

-

the end of the original range

policy

-

the execution policy to use. See execution policy for details.

类型要求

---。

-取消引用的前向的类型必须符合可移动分配和移动可建的要求。

返回值

(none).

(until C++11)

The iterator equal to first + (last - n_first).

(since C++11)

复杂性

直线在之间的距离firstlast...

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

模板<class ForwardIt>向前旋转%28前进优先,向前旋转n[医]第一,前进最后%29{如果%28first==n[医]第一%29返回最后;如果%28 N[医]首先==最后%29,先返回;向前,下一步=n[医]第一;DO{STD::ITER[医]交换%281++,Next++%29;if%281==n[医]第一%29 n[医]First=Next;}而%28Next%21=最后%29;Forwardit rett=First;for%28Next=n[医]第一;下一%21=最后;%29{std::ITER[医]交换%281++,Next++%29;if%281==n[医]第一%29 n[医]首先=Next;否则如果%28Next=最后%29 Next=n[医]首先;}返回RET;}

*。

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

二次

代码语言:javascript
复制
#include <vector>
#include <iostream>
#include <algorithm>
 
int main()
{
    std::vector<int> v{2, 4, 2, 0, 5, 10, 7, 3, 7, 1}; 
 
    std::cout << "before sort:      ";
    for (int n: v)
        std::cout << n << ' ';
    std::cout << '\n';
 
    // insertion sort
    for (auto i = v.begin(); i != v.end(); ++i) {
        std::rotate(std::upper_bound(v.begin(), i, *i), i, i+1);
    }
 
    std::cout << "after sort:       ";
    for (int n: v)
        std::cout << n << ' ';
    std::cout << '\n';
 
    // simple rotation to the left
    std::rotate(v.begin(), v.begin() + 1, v.end());
 
    std::cout << "simple rotate left  : ";
    for (int n: v)
        std::cout << n << ' ';
    std::cout << '\n';
 
    // simple rotation to the right
    std::rotate(v.rbegin(), v.rbegin() + 1, v.rend());
 
    std::cout << "simple rotate right : ";
    for (int n: v)
        std::cout << n << ' ';
    std::cout << '\n';
 
}

二次

产出:

二次

代码语言:javascript
复制
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

二次

另见

rotate_copy

copies and rotate a range of elements (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券