std::pair<T1,T2>::swap
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
void swap(pair& other) noexcept(/* see below */); |
(C++11以上) (C++20未満) |
|
constexpr void swap(pair& other) noexcept(/* see below */); |
(C++20以上) | |
first を other.first と、 second を other.second と入れ替えます。
引数
| other | - | 入れ替える値のペア |
戻り値
(なし)
例外
|
noexcept 指定:
noexcept( noexcept(swap(first, other.first)) && noexcept(swap(second, other.second)) )上の式において、識別子 |
(C++17未満) |
|
noexcept 指定:
noexcept( std::is_nothrow_swappable_v<first_type> && std::is_nothrow_swappable_v<second_type> ) |
(C++17以上) |
例
Run this code
#include <iostream>
#include <utility>
#include <string>
int main()
{
std::pair<int, std::string> p1, p2;
p1 = std::make_pair(10, "test");
p2.swap(p1);
std::cout << "(" << p2.first << ", " << p2.second << ")\n";
}
出力:
(10, test)
欠陥報告
以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。
| DR | 適用先 | 発行時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2456 | C++11 | the noexcept specification is ill-formed
|
made to work |
関連項目
| 2つのオブジェクトの値を入れ替えます (関数テンプレート) |