|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
(C++11) | ||||
| Non-member functions | ||||
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20) | ||||
swap(std::pair) (C++11) | ||||
(C++11) | ||||
| Helper classes | ||||
(C++11) | ||||
(C++11) | ||||
(C++23) | ||||
(C++23) | ||||
(C++11) | ||||
| Deduction guides(C++17) |
Defined in header <utility> | ||
| (1) | ||
| (since C++11) (until C++20) | ||
| (since C++20) | ||
| (2) | (since C++23) | |
Swaps the contents ofx andy. Equivalent tox.swap(y).
1) This overload participates in overload resolution only ifstd::is_swappable_v<first_type>&&std::is_swappable_v<second_type> istrue. 2) This overload participates in overload resolution only ifstd::is_swappable_v<const first_type>&&std::is_swappable_v<const second_type> istrue. | (since C++17) |
Contents |
| x, y | - | pairs whose contents to swap |
(none)
#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): ");}
Output:
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}| swaps the values of two objects (function template)[edit] | |
(C++11) | specializes thestd::swap algorithm (function template)[edit] |