Namespaces
Variants

std:: swap_ranges

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
swap_ranges

Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
template < class ForwardIt1, class ForwardIt2 >

ForwardIt2 swap_ranges ( ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2 ) ;
(1) (自 C++20 起为 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2 >
ForwardIt2 swap_ranges ( ExecutionPolicy && policy,
ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2 ) ;
(2) (自 C++17 起)
1) 交换范围 [ first1 , last1 ) 与起始于 first2 的另一个包含 std:: distance ( first1, last1 ) 个元素的区间之间的元素。
2) (1) 相同,但根据 policy 执行。
此重载仅在满足以下所有条件时参与重载决议:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true

(C++20 前)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true

(C++20 起)

若满足以下任一条件,则行为未定义:

  • 两个范围存在重叠。
  • 在两个范围中存在对应的迭代器 iter1 iter2 ,使得 * iter1 * iter2 不可 交换

目录

参数

first1, last1 - 定义待交换元素 范围 的迭代器对
first2 - 待交换的第二个元素范围的起始位置
policy - 使用的 执行策略
类型要求
-
ForwardIt1, ForwardIt2 必须满足 LegacyForwardIterator 的要求。

返回值

指向范围中最后一个被交换元素之后元素的迭代器,该范围起始于 first2

复杂度

恰好进行 std:: distance ( first1, last1 ) 次交换。

异常

带有名为 ExecutionPolicy 模板参数的重载按以下方式报告错误:

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

注释

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

可能的实现

template<class ForwardIt1, class ForwardIt2>
constexpr //< since C++20
ForwardIt2 swap_ranges(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2)
{
    for (; first1 != last1; ++first1, ++first2)
        std::iter_swap(first1, first2);
    return first2;
}

示例

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

#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
auto print = [](auto comment, auto const& seq)
{
    std::cout << comment;
    for (const auto& e : seq)
        std::cout << e << ' ';
    std::cout << '\n';
};
int main()
{
    std::vector<char> v{'a', 'b', 'c', 'd', 'e'};
    std::list<char> l{'1', '2', '3', '4', '5'};
    print("Before swap_ranges:\n" "v: ", v);
    print("l: ", l);
    std::swap_ranges(v.begin(), v.begin() + 3, l.begin());
    print("After swap_ranges:\n" "v: ", v);
    print("l: ", l);
}

输出:

Before swap_ranges:
v: a b c d e
l: 1 2 3 4 5
After swap_ranges:
v: 1 2 3 d e
l: a b c 4 5

参见

交换两个迭代器所指向的元素
(函数模板)
交换两个对象的值
(函数模板)
交换两个元素范围
(算法函数对象)