std::swap(std::pair)
From cppreference.com
| ヘッダ <utility> で定義 |
||
| (1) | ||
| (C++11以降) (C++20まで) |
||
| (C++20以降) | ||
| (2) | (C++23から) | |
x と y の内容を交換します。x.swap(y) と同等です。
|
1) このオーバーロードは、std::is_swappable_v<first_type> && std::is_swappable_v<second_type> が true の場合にのみ、オーバーロード解決に参加します。
2) このオーバーロードは、std::is_swappable_v<const first_type> && std::is_swappable_v<const second_type> が true の場合にのみ、オーバーロード解決に参加します。
|
(C++17以降) |
目次 |
[編集] Parameters
| x, y | - | 交換する内容を持つペア |
[編集] Return value
(なし)
[編集] Exceptions
noexcept 指定:
noexcept(noexcept(x.swap(y)))
[編集] Example
このコードを実行
#include <iostream> #include <utility> int main() { auto p1 = std::make_pair(10, 3.14); auto p2 = std::pair(12, 1.23); // CTAD, since C++17 auto print_p1_p2 = [&](auto msg) { std::cout << msg << "p1 = {" << std::get<0>(p1) << ", " << std::get<1>(p1) << "}, " << "p2 = {" << std::get<0>(p2) << ", " << std::get<1>(p2) << "}\n"; }; print_p1_p2("Before p1.swap(p2): "); p1.swap(p2); print_p1_p2("After p1.swap(p2): "); std::swap(p1, p2); print_p1_p2("After swap(p1, p2): "); }
出力
Before p1.swap(p2): p1 = {10, 3.14}, p2 = {12, 1.23}
After p1.swap(p2): p1 = {12, 1.23}, p2 = {10, 3.14}
After swap(p1, p2): p1 = {10, 3.14}, p2 = {12, 1.23}[編集] See also
| 2つのオブジェクトの値を交換する (関数テンプレート) | |
| (C++11) |
std::swap アルゴリズムを特殊化する (関数テンプレート) |