名前空間
変種

std::pair<T1,T2>::swap

提供: cppreference.com
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
std::pair
メンバ関数
非メンバ関数
(C++20未満)(C++20未満)(C++20未満)(C++20未満)(C++20未満)(C++20)
(C++11)
推定ガイド(C++17)
ヘルパークラス
(C++11)
 
<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以上)

firstother.first と、 secondother.second と入れ替えます。

引数

other - 入れ替える値のペア

戻り値

(なし)

例外

noexcept 指定:  
noexcept( noexcept(swap(first, other.first)) && noexcept(swap(second, other.second)) )

上の式において、識別子 swap は C++17 の std::is_nothrow_swappable 特性で使用されるものと同じ方法で探索されます。

(C++17未満)
noexcept 指定:  
noexcept( std::is_nothrow_swappable_v<first_type> && std::is_nothrow_swappable_v<second_type> )
(C++17以上)

#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つのオブジェクトの値を入れ替えます
(関数テンプレート) [edit]