Namespaces
Variants

std:: swap

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
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>
(C++11 前)
定义于头文件 <utility>
(C++11 起)
定义于头文件 <string_view>
template < class T >
void swap ( T & a, T & b ) ;
(1) (有条件 noexcept 自 C++11)
(constexpr 自 C++20)
template < class T2, std:: size_t N >
void swap ( T2 ( & a ) [ N ] , T2 ( & b ) [ N ] ) ;
(2) (有条件 noexcept 自 C++11)
(constexpr 自 C++20)

交换给定的值。

1) 交换 a b 的值。

此重载仅当 std:: is_move_constructible_v < T > && std:: is_move_assignable_v < T > true 时参与重载决议。

(since C++17)
2) 交换数组 a b 。等价于 std:: swap_ranges ( a, a + N, b )

此重载仅当 std:: is_swappable_v < T2 > true 时参与重载决议。

(C++17 起)

目录

参数

a, b - 需要交换的值
类型要求
-
T 必须满足 CopyConstructible CopyAssignable (C++11 前) MoveConstructible MoveAssignable (C++11 起) 的要求。
-
T2 必须满足 Swappable 的要求。

返回值

(无)

异常

1)

(无)

(C++11 前)
noexcept 规范:
(C++11 起)
2)
noexcept 规范:
noexcept ( noexcept ( swap ( * a, * b ) ) )
在异常规范中对标识符 swap 的查找,除了通过常规查找规则找到的内容外,还会找到此函数模板,这使得异常规范等效于 C++17 的 std::is_nothrow_swappable
(C++11 起)
(C++17 前)
noexcept 规范:
noexcept ( std:: is_nothrow_swappable_v < T2 > )
(C++17 起)

复杂度

1) 常量。
2) 关于 N 呈线性。

特化

std::swap 可为 程序定义类型在命名空间 std 中进行特化 ,但此类特化无法通过 ADL 找到(命名空间 std 不是程序定义类型的关联命名空间)。

(C++20 前)

使 程序定义类型 可交换的预期方式是在该类型所属的命名空间中提供非成员函数 swap:详见 Swappable

标准库已提供以下重载:

特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)

示例

#include <algorithm>
#include <iostream>
namespace Ns
{
    class A
    {
        int id {};
        friend void swap(A& lhs, A& rhs)
        {
            std::cout << "swap(" << lhs << ", " << rhs << ")\n";
            std::swap(lhs.id, rhs.id);
        }
        friend std::ostream& operator<<(std::ostream& os, A const& a)
        {
            return os << "A::id=" << a.id;
        }
    public:
        A(int i) : id {i} {}
        A(A const&) = delete;
        A& operator = (A const&) = delete;
    };
}
int main()
{
    int a = 5, b = 3;
    std::cout << a << ' ' << b << '\n';
    std::swap(a, b);
    std::cout << a << ' ' << b << '\n';
    Ns::A p {6}, q {9};
    std::cout << p << ' ' << q << '\n';
//  std::swap(p, q); // 错误:类型要求未满足
    swap(p, q);      // 正确:ADL找到了合适的友元函数 `swap`
    std::cout << p << ' ' << q << '\n';
}

输出:

5 3
3 5
A::id=6 A::id=9
swap(A::id=6, A::id=9)
A::id=9 A::id=6

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 应用于 发布时行为 正确行为
LWG 227 C++98 T 未被要求为 CopyConstructible DefaultConstructible
(可能无法构造 T 类型的临时对象)
同时要求 T
CopyConstructible
LWG 809 C++98 无法交换数组 添加重载 (2)
LWG 2554 C++11 由于名称查找问题,交换多维数组永远
不能声明为 noexcept
已修复使其正常工作

参见

交换两个对象的值
(定制点对象)
交换两个迭代器所指向的元素
(函数模板)
交换两个元素范围
(函数模板)
(C++14)
用新值替换参数并返回其先前值
(函数模板)