std::swap_ranges
来自cppreference.com
![]() | 该页由英文版wiki使用Google Translate机器翻译而来。 该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <algorithm> 中定义 | ||
template<class ForwardIt1, class ForwardIt2 > ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 ) | ||
交易要素之间的范围
[first1, last1)
和另一范围,开始在first2
. 原文:
Exchanges elements between range
[first1, last1)
and another range starting at first2
. 目录 |
[编辑]参数
first1, last1 | - | 第一个范围的元素交换 |
first2 | - | 年初第二交换的元素 原文: beginning of the second range of elements to swap |
类型要求 | ||
-ForwardIt1, ForwardIt2 必须满足 ForwardIterator 的要求。 | ||
-The types of dereferenced ForwardIt1 and ForwardIt2 must meet the requirements of Swappable |
[编辑]返回值
迭代器交换的范围内开始,
first2
过去的最后一个元素的元素.原文:
Iterator to the element past the last element exchanged in the range beginning with
first2
.[编辑]可能的实现
template<class ForwardIt1, class ForwardIt2> ForwardIt1 swap_ranges(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2){while(first1 != last1){std::iter_swap(first1++, first2++);}return first2;} |
[编辑]示例
演示如何交换子范围从不同的容器
原文:
Demonstrates swapping of subranges from different containers
#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';}
输出:
-1 -2 -3 4 5 1 2 3 -4 -5
[编辑]复杂度
线性
first
和last
之间的距离原文:
linear in the distance between
first
and last
[编辑]另请参阅
交换两个迭代器所指向的元素 (函数模板) | |
交换两个对象的值 (函数模板) |