std::swap(std::pair)(3)
π std::swap(std::pair) β yeah bestie, we're swapping pairs today
okay so this is the std::swap page for std::pair, straight out of the C++ Standard Library. it lives in <utility> and it does exactly one thing extremely well: swaps two pairs. no cap. π«Ά
β¨ NAME β¨
std::swap(std::pair) β literally std::swap(std::pair), that's the whole name, she's not complicated π
πΈ SYNOPSIS πΈ
Defined in header <utility>
template< class T1, class T2 >
void swap( std::pair<T1,T2>& x, std::pair<T1,T2>& y )
noexcept(/* see below */);
(since C++11) (until C++20)
template< class T1, class T2 >
constexpr void swap( std::pair<T1,T2>& x, std::pair<T1,T2>& y )
noexcept(/* see below */);
(1) (since C++20)
template< class T1, class T2 >
constexpr void swap( const std::pair<T1,T2>& x, const std::pair<T1,T2>& y )
noexcept(/* see below */);
(2) (since C++23)
π¦ DESCRIPTION π¦
Swaps the contents of x and y. That's it. It's equivalent to x.swap(y). iconic behavior, honestly. β¨
1)this overload only participates in overload resolution ifstd::is_swappable_v<first_type> && std::is_swappable_v<second_type>istrue. (since C++17)2)this overload only participates in overload resolution ifstd::is_swappable_v<const first_type> && std::is_swappable_v<const second_type>istrue.
π§ friendly reminder that those two "only participates ifβ¦" clauses are literally part of the spec β she's picky about overload resolution and honestly? valid.
π PARAMETERS π
x,yβ the pairs whose contents to swap π«Ά
π RETURN VALUE π
(none) β she gives you nothing back, she just does the thing. main character energy tbh.
π₯ EXCEPTIONS π₯
the noexcept specification is:
noexcept(noexcept(x.swap(y)))
β EXAMPLE β
// Run this code
#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}
likeβ¦ she swapped twice and ended up right back where she started. we love a round trip ππ
π SEE ALSO π
swapβ swaps the values of two objects (function template)std::swap(std::tuple)(C++11) β specializes thestd::swapalgorithm (function template)
π Category π
*conditionallynoexcept
aaaaaaaaand that's the page!! π¦ go forth and swap your pairs, bestie, you're gonna slay. no notes. π
source: cppreference.com β version 2024.06.10 β mwah, kisses, see u next page πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms