std::pair<T1,T2>::swap
From cppreference.com
| (1) | ||
void swap( pair& other ) noexcept(/* 以下参照 */); |
(C++11以降) (C++20まで) |
|
| constexpr void swap( pair& other ) noexcept(/* 以下参照 */); |
(C++20以降) | |
| constexpr void swap( const pair& other ) const noexcept(/* 以下参照 */); |
(2) | (C++23から) |
first と other.first、および second と other.second を入れ替える。これは、using std::swap; swap(first, other.first); swap(second, other.second); のように行われる。
|
選択された |
(C++23まで) |
|
選択された |
(C++23から) |
目次 |
[編集] パラメータ
| その他 | - | 入れ替える値のペア |
[編集] 戻り値
(なし)
[編集] 例外
|
noexcept 指定:
noexcept( noexcept(swap(first, other.first)) && 上記の式において、識別子 |
(C++17まで) |
|
1)
noexcept 指定: noexcept( std::is_nothrow_swappable_v<first_type> && 2) noexcept 指定:
noexcept( std::is_nothrow_swappable_v<const first_type> && |
(C++17以降) |
[編集] 例
このコードを実行
#include <iostream> #include <utility> #include <string> int main() { std::pair<int, std::string> p1(10, "test"), p2; p2.swap(p1); std::cout << "(" << p2.first << ", " << p2.second << ")\n"; #if __cpp_lib_ranges_zip >= 202110L // Using the C++23 const qualified swap overload // (swap is no longer propagating pair constness) int i1 = 10, i2{}; std::string s1("test"), s2; const std::pair<int&, std::string&> r1(i1, s1), r2(i2, s2); r2.swap(r1); std::cout << "(" << i2 << ", " << s2 << ")\n"; #endif }
実行結果の例
(10, test) (10, test)
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2456 | C++11 | noexcept指定が不正形式 |
動作するように修正 |
[編集] 関連項目
| 2つのオブジェクトの値を交換する (関数テンプレート) | |
2つの tuple の内容を入れ替える( std::tuple<Types...> の public メンバ関数) |