跳到主要内容

std::swap() 算法

// (1)
template< class T >
constexpr void swap( T& a, T& b ) noexcept(/* see below */);

// (2)
template< class T2, std::size_t N >
constexpr void swap( T2 (&a)[N], T2 (&b)[N] ) noexcept(/* see below */);

交换给定值。

  • (1) 交换值 ab

    重载决议
    此重载不参与重载决议,除非 std::is_move_constructible_v<T> && std::is_move_assignable_v<T>true (自 C++17 起)
  • (2) 交换数组 ab。实际上调用 std::swap_ranges(a, a + N, b)

    重载决议
    此重载不参与重载决议,除非 std::is_swappable_v<T2>true (自 C++17 起)

参数

a
b

要交换的值。

类型要求

T 可移动构造, 可移动赋值 (自 C++11 起)
可复制构造, 可复制赋值 (直到 C++11)
T2可交换

返回值

(无)

复杂度

  • (1) - 常量。
  • (1) - N 的线性。

异常

C++11 之前不抛出异常。

  • (1)

    noexcept 规范

    noexcept(
    std::is_nothrow_move_constructible<T>::value &&
    std::is_nothrow_move_assignable<T>::value
    )
  • (2)

    noexcept 规范

    noexcept(std::is_nothrow_swappable_v<T2>)

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数抛出异常,并且 ExecutionPolicy标准策略之一,则调用 std::terminate。对于其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

特化

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

使程序定义类型可交换的预期方法是在与类型相同的命名空间中提供非成员函数 swap:有关详细信息,请参见可交换

标准库已提供以下重载

std::swap(std::pair)

特化 std::swap 算法以用于 std::pair

std::swap(std::tuple) (自 C++11 起)

特化 std::swap 算法以用于 std::tuple

std::swap(std::shader_ptr) (自 C++11 起)

特化 std::swap 算法以用于 std::shader_ptr

std::swap(std::weak_ptr) (自 C++11 起)

特化 std::swap 算法以用于 std::weak_ptr

std::swap(std::unique_ptr) (自 C++11 起)

特化 std::swap 算法以用于 std::unique_ptr

std::swap(std::function) (自 C++11 起)

特化 std::swap 算法以用于 std::function

std::swap(std::basic_string)

特化 std::swap 算法以用于 std::basic_string

std::swap(std::array) (自 C++11 起)

特化 std::swap 算法以用于 std::array

std::swap(std::deque) (自 C++11 起)

特化 std::swap 算法以用于 std::deque

std::swap(std::forward_list) (自 C++11 起)

特化 std::swap 算法以用于 std::forward_list

std::swap(std::list)

特化 std::swap 算法以用于 std::list

std::swap(std::vector)

特化 std::swap 算法以用于 std::vector

std::swap(std::map)

特化 std::swap 算法以用于 std::map

std::swap(std::multimap)

特化 std::swap 算法以用于 std::multimap

std::swap(std::set)

特化 std::swap 算法以用于 std::set

std::swap(std::multiset)

特化 std::swap 算法以用于 std::multiset

std::swap(std::unordered_map) (自 C++11 起)

特化 std::swap 算法以用于 std::unordered_map

std::swap(std::unordered_multimap) (自 C++11 起)

特化 std::swap 算法以用于 std::unordered_multimap

std::swap(std::unordered_set) (自 C++11 起)

特化 std::swap 算法以用于 std::unordered_set

std::swap(std::unordered_multiset) (自 C++11 起)

特化 std::swap 算法以用于 std::unordered_multiset

std::swap(std::queue) (自 C++11 起)

特化 std::swap 算法以用于 std::queue

std::swap(std::priority_queue) (自 C++11 起)

特化 std::swap 算法以用于 std::priority_queue

std::swap(std::stack) (自 C++11 起)

特化 std::swap 算法以用于 std::stack

std::swap(std::valarray) (自 C++11 起)

特化 std::swap 算法以用于 std::valarray

std::swap(std::basic_stringbuf) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_stringbuf

std::swap(std::basic_istringstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_istringstream

std::swap(std::basic_ostringstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_ostringstream

std::swap(std::basic_stringstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_stringstream

std::swap(std::basic_filebuf) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_filebuf

std::swap(std::basic_ifstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_ifstream

std::swap(std::basic_ofstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_ofstream

std::swap(std::basic_fstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_fstream

std::swap(std::basic_syncbuf) (自 C++20 起)

特化 std::swap 算法以用于 std::basic_syncbuf

std::swap(std::basic_spanbuf) (自 C++23 起)

特化 std::swap 算法以用于 std::basic_spanbuf

std::swap(std::basic_ispanstream) (自 C++23 起)

特化 std::swap 算法以用于 std::basic_ispanstream

std::swap(std::basic_ospanstream) (自 C++23 起)

特化 std::swap 算法以用于 std::basic_ospanstream

std::swap(std::basic_spanstream) (自 C++23 起)

特化 std::swap 算法以用于 std::basic_spanstream

std::swap(std::basic_regex) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_regex

std::swap(std::match_results) (自 C++11 起)

特化 std::swap 算法以用于 std::match_results

std::swap(std::thread) (自 C++11 起)

特化 std::swap 算法以用于 std::thread

std::swap(std::unique_lock) (自 C++11 起)

特化 std::swap 算法以用于 std::unique_lock

std::swap(std::shared_lock) (自 C++14 起)

特化 std::swap 算法以用于 std::shared_lock

std::swap(std::promise) (自 C++11 起)

特化 std::swap 算法以用于 std::promise

std::swap(std::packaged_task) (自 C++11 起)

特化 std::swap 算法以用于 std::packaged_task

std::swap(std::optional) (自 C++17 起)

特化 std::swap 算法以用于 std::optional

std::swap(std::any) (自 C++17 起)

特化 std::swap 算法以用于 std::any

std::swap(std::variant) (自 C++17 起)

特化 std::swap 算法以用于 std::variant

std::swap(std::basic_stacktrace) (自 C++17 起)

特化 std::swap 算法以用于 std::basic_stacktrace

swap(std::filesystem::path) (自 C++17 起)

特化(隐藏朋友)std::swap 算法以用于 std::filesystem::path

swap(std::expected) (自 C++23 起)

特化(隐藏朋友)std::swap 算法以用于 std::expected

swap(std::jthread) (自 C++20 起)

特化(隐藏朋友)std::swap 算法以用于 std::jthread

swap(std::move_only_function) (自 C++23 起)

特化(隐藏朋友)std::swap 算法以用于 std::move_only_function

swap(std::stop_source) (自 C++20 起)

特化(隐藏朋友)std::swap 算法以用于 std::stop_source

swap(std::stop_token) (自 C++20 起)

特化(隐藏朋友)std::swap 算法以用于 std::stop_token

示例

Main.cpp
#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); // error, type requirements are not satisfied
swap(p, q); // OK, ADL finds the appropriate friend `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
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”以查看此文档的所有更改。
悬停查看原始许可证。

std::swap() 算法

// (1)
template< class T >
constexpr void swap( T& a, T& b ) noexcept(/* see below */);

// (2)
template< class T2, std::size_t N >
constexpr void swap( T2 (&a)[N], T2 (&b)[N] ) noexcept(/* see below */);

交换给定值。

  • (1) 交换值 ab

    重载决议
    此重载不参与重载决议,除非 std::is_move_constructible_v<T> && std::is_move_assignable_v<T>true (自 C++17 起)
  • (2) 交换数组 ab。实际上调用 std::swap_ranges(a, a + N, b)

    重载决议
    此重载不参与重载决议,除非 std::is_swappable_v<T2>true (自 C++17 起)

参数

a
b

要交换的值。

类型要求

T 可移动构造, 可移动赋值 (自 C++11 起)
可复制构造, 可复制赋值 (直到 C++11)
T2可交换

返回值

(无)

复杂度

  • (1) - 常量。
  • (1) - N 的线性。

异常

C++11 之前不抛出异常。

  • (1)

    noexcept 规范

    noexcept(
    std::is_nothrow_move_constructible<T>::value &&
    std::is_nothrow_move_assignable<T>::value
    )
  • (2)

    noexcept 规范

    noexcept(std::is_nothrow_swappable_v<T2>)

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数抛出异常,并且 ExecutionPolicy标准策略之一,则调用 std::terminate。对于其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

特化

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

使程序定义类型可交换的预期方法是在与类型相同的命名空间中提供非成员函数 swap:有关详细信息,请参见可交换

标准库已提供以下重载

std::swap(std::pair)

特化 std::swap 算法以用于 std::pair

std::swap(std::tuple) (自 C++11 起)

特化 std::swap 算法以用于 std::tuple

std::swap(std::shader_ptr) (自 C++11 起)

特化 std::swap 算法以用于 std::shader_ptr

std::swap(std::weak_ptr) (自 C++11 起)

特化 std::swap 算法以用于 std::weak_ptr

std::swap(std::unique_ptr) (自 C++11 起)

特化 std::swap 算法以用于 std::unique_ptr

std::swap(std::function) (自 C++11 起)

特化 std::swap 算法以用于 std::function

std::swap(std::basic_string)

特化 std::swap 算法以用于 std::basic_string

std::swap(std::array) (自 C++11 起)

特化 std::swap 算法以用于 std::array

std::swap(std::deque) (自 C++11 起)

特化 std::swap 算法以用于 std::deque

std::swap(std::forward_list) (自 C++11 起)

特化 std::swap 算法以用于 std::forward_list

std::swap(std::list)

特化 std::swap 算法以用于 std::list

std::swap(std::vector)

特化 std::swap 算法以用于 std::vector

std::swap(std::map)

特化 std::swap 算法以用于 std::map

std::swap(std::multimap)

特化 std::swap 算法以用于 std::multimap

std::swap(std::set)

特化 std::swap 算法以用于 std::set

std::swap(std::multiset)

特化 std::swap 算法以用于 std::multiset

std::swap(std::unordered_map) (自 C++11 起)

特化 std::swap 算法以用于 std::unordered_map

std::swap(std::unordered_multimap) (自 C++11 起)

特化 std::swap 算法以用于 std::unordered_multimap

std::swap(std::unordered_set) (自 C++11 起)

特化 std::swap 算法以用于 std::unordered_set

std::swap(std::unordered_multiset) (自 C++11 起)

特化 std::swap 算法以用于 std::unordered_multiset

std::swap(std::queue) (自 C++11 起)

特化 std::swap 算法以用于 std::queue

std::swap(std::priority_queue) (自 C++11 起)

特化 std::swap 算法以用于 std::priority_queue

std::swap(std::stack) (自 C++11 起)

特化 std::swap 算法以用于 std::stack

std::swap(std::valarray) (自 C++11 起)

特化 std::swap 算法以用于 std::valarray

std::swap(std::basic_stringbuf) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_stringbuf

std::swap(std::basic_istringstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_istringstream

std::swap(std::basic_ostringstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_ostringstream

std::swap(std::basic_stringstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_stringstream

std::swap(std::basic_filebuf) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_filebuf

std::swap(std::basic_ifstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_ifstream

std::swap(std::basic_ofstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_ofstream

std::swap(std::basic_fstream) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_fstream

std::swap(std::basic_syncbuf) (自 C++20 起)

特化 std::swap 算法以用于 std::basic_syncbuf

std::swap(std::basic_spanbuf) (自 C++23 起)

特化 std::swap 算法以用于 std::basic_spanbuf

std::swap(std::basic_ispanstream) (自 C++23 起)

特化 std::swap 算法以用于 std::basic_ispanstream

std::swap(std::basic_ospanstream) (自 C++23 起)

特化 std::swap 算法以用于 std::basic_ospanstream

std::swap(std::basic_spanstream) (自 C++23 起)

特化 std::swap 算法以用于 std::basic_spanstream

std::swap(std::basic_regex) (自 C++11 起)

特化 std::swap 算法以用于 std::basic_regex

std::swap(std::match_results) (自 C++11 起)

特化 std::swap 算法以用于 std::match_results

std::swap(std::thread) (自 C++11 起)

特化 std::swap 算法以用于 std::thread

std::swap(std::unique_lock) (自 C++11 起)

特化 std::swap 算法以用于 std::unique_lock

std::swap(std::shared_lock) (自 C++14 起)

特化 std::swap 算法以用于 std::shared_lock

std::swap(std::promise) (自 C++11 起)

特化 std::swap 算法以用于 std::promise

std::swap(std::packaged_task) (自 C++11 起)

特化 std::swap 算法以用于 std::packaged_task

std::swap(std::optional) (自 C++17 起)

特化 std::swap 算法以用于 std::optional

std::swap(std::any) (自 C++17 起)

特化 std::swap 算法以用于 std::any

std::swap(std::variant) (自 C++17 起)

特化 std::swap 算法以用于 std::variant

std::swap(std::basic_stacktrace) (自 C++17 起)

特化 std::swap 算法以用于 std::basic_stacktrace

swap(std::filesystem::path) (自 C++17 起)

特化(隐藏朋友)std::swap 算法以用于 std::filesystem::path

swap(std::expected) (自 C++23 起)

特化(隐藏朋友)std::swap 算法以用于 std::expected

swap(std::jthread) (自 C++20 起)

特化(隐藏朋友)std::swap 算法以用于 std::jthread

swap(std::move_only_function) (自 C++23 起)

特化(隐藏朋友)std::swap 算法以用于 std::move_only_function

swap(std::stop_source) (自 C++20 起)

特化(隐藏朋友)std::swap 算法以用于 std::stop_source

swap(std::stop_token) (自 C++20 起)

特化(隐藏朋友)std::swap 算法以用于 std::stop_token

示例

Main.cpp
#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); // error, type requirements are not satisfied
swap(p, q); // OK, ADL finds the appropriate friend `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
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”以查看此文档的所有更改。
悬停查看原始许可证。