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

std::swap_ranges

Defined in header <algorithm>

?

?

template< class ForwardIt1, class ForwardIt2 > ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );

(1)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt2 swap_ranges( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );

(2)

(since C++17)

1%29在范围间交换元素[first1, last1)另一个范围从first2...

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

参数

first1, last1

-

the first range of elements to swap

first2

-

beginning of the second range of elements to swap

policy

-

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

类型要求

-前进1,前进2必须符合先行者的要求。

-取消引用的前进It 1和Forward It 2的类型必须符合可互换的要求。

返回值

元素的Iterator在范围内交换的最后一个元素开始于first2...

例外

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

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

可能的实施

模板<类ForwardIt 1,类ForwardIt 2>ForwardIt 2交换[医]范围%28ForwardIt1 First 1、ForwardIt 1 last1、ForwardIt 2 First 2%29{而%28first 1%21=last1%29{std::ITER[医]交换%28first 1++,头2++%29;}返回First 2;}

*。

演示来自不同容器的子范围的交换。

二次

代码语言:javascript
复制
#include <algorithm>
#include <list>
#include <vector>
#include <iostream>
int main()
{
    std::vector<int> v = {1, 2, 3, 4, 5};
    std::list<int> l = {-1, -2, -3, -4, -5};
 
    std::swap_ranges(v.begin(), v.begin()+3, l.begin());
 
    for(int n : v)
       std::cout << n << ' ';
    std::cout << '\n';
    for(int n : l)
       std::cout << n << ' ';
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
-1 -2 -3 4 5
1 2 3 -4 -5

二次

复杂性

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

另见

iter_swap

swaps the elements pointed to by two iterators (function template)

swap

swaps the values of two objects (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com