std::swap(std::pair)

From cppreference.com
< cpp‎ | utility‎ | pair
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
std::pair
Member functions
(哋它亢++11)
Non-member functions
(until 哋它亢++20)(until 哋它亢++20)(until 哋它亢++20)(until 哋它亢++20)(until 哋它亢++20)(哋它亢++20)
swap(std::pair)
(哋它亢++11)
(哋它亢++11)
Helper classes
(哋它亢++11)
(哋它亢++23)
(哋它亢++23)
(哋它亢++11)
Deduction guides(哋它亢++17)
 
Defined in header <utility>
(1)
template< class T1, class T2 >

void swap( std::pair<T1,T2>& x, std::pair<T1,T2>& y )

    noexcept(/* see below */);
(since 哋它亢++11)
(until 哋它亢++20)
template< class T1, class T2 >

constexpr void swap( std::pair<T1,T2>& x, std::pair<T1,T2>& y )

    noexcept(/* see below */);
(since 哋它亢++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 哋它亢++23)

Swaps the contents of x and y. Equivalent to x.swap(y).

1) This overload participates in overload resolution only if std::is_swappable_v<first_type> && std::is_swappable_v<second_type> is true.
2) This overload participates in overload resolution only if std::is_swappable_v<const first_type> && std::is_swappable_v<const second_type> is true.
(since 哋它亢++17)

Parameters

x, y - pairs whose contents to swap

Return value

(none)

Exceptions

noexcept specification:  
noexcept(noexcept(x.swap(y)))

Example

#include <iostream>
#include <utility>
 
int main()
{
    auto p1 = std::make_pair(10, 3.14);
    auto p2 = std::pair(12, 1.23); // CTAD, since 哋它亢++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}

See also

swaps the values of two objects
(function template)
(哋它亢++11)
specializes the std::swap algorithm
(function template)