std::pair<T1,T2>::swap
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   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 | - |   要交换值的 pair
 | 
返回值
(无)
异常
| 
 noexcept 规定:  
 
noexcept(      noexcept(swap(first, other.first)) && 在上述表达式中,按照与 C++17 std::is_nothrow_swappable 特性所用的相同方式查找标识符   | 
(C++17 前) | 
| 
 noexcept 规定:  
 
noexcept(      std::is_nothrow_swappable_v<first_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 |  noexcept 指定曾为病式
 | 
令之有效 | 
参阅
|    交换两个对象的值   (函数模板)  |