Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/pair/swap2"

From cppreference.com
< cpp‎ | utility‎ | pair
(Created page with "{{cpp/title | swap<small>(std::pair</small>}} {{cpp/utility/pair/sidebar}} {{ddcl list begin}} {{ddcl list item | template<class T1, class T2> void swap(pair<T1, T2>& x, pair<T1,...")
 
m (fmt.)
 
(22 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{cpp/title | swap<small>(std::pair</small>}}
+
{{cpp/title|swap<small>(std::pair)</small>}}
{{cpp/utility/pair/sidebar}}
+
{{cpp/utility/pair/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list item |
+
{{dcl header|utility}}
template<class T1, class T2>
+
{{dcl rev multi|num=1
void swap(pair<T1, T2>& x, pair<T1, T2>& y);
+
|since1=c++11|dcl1=
 +
template< class T1, class T2 >
 +
void swap( std::pair<T1,T2>& x, std::pair<T1,T2>& y )
 +
    noexcept(/* see below */);
 +
|since2=c++20|dcl2=
 +
template< class T1, class T2 >
 +
constexpr void swap( std::pair<T1,T2>& x, std::pair<T1,T2>& y )
 +
    noexcept(/* see below */);
 
}}
 
}}
{{ddcl list end}}
+
{{dcl|num=2
 +
|since=c++23|
 +
template< class T1, class T2 >
 +
constexpr void swap( const std::pair<T1,T2>& x, const std::pair<T1,T2>& y )
 +
    noexcept(/* see below */);
 +
}}
 +
{{dcl end}}
  
Swaps the contents of {{tt|lhs}} and {{tt|rhs}}. Eqiivalent to {{cpp|lhs.swap(rhs)}}.
+
Swaps the contents of {{tt|x}} and {{tt|y}}. Equivalent to {{c|x.swap(y)}}.
 +
 
 +
{{rrev|since=c++17|
 +
@1@ {{cpp/enable_if|{{c|std::is_swappable_v<first_type> && std::is_swappable_v<second_type>}} is {{c|true}}}}.
 +
 
 +
@2@ {{cpp/enable_if|{{c|std::is_swappable_v<const first_type> && std::is_swappable_v<const second_type>}} is {{c|true}}}}.
 +
}}
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | lhs, rhs | pairs whose contents to swap}}
+
{{par|x, y|pairs whose contents to swap}}
{{param list end}}  
+
{{par end}}
  
 
===Return value===
 
===Return value===
{{return none}}
+
(none)
  
 
===Exceptions===
 
===Exceptions===
 
{{noexcept|noexcept(x.swap(y))}}
 
{{noexcept|noexcept(x.swap(y))}}
 +
 +
===Example===
 +
{{example|
 +
|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}
 +
}}
  
 
===See also===
 
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/algorithm/dsc swap}}
 +
{{dsc inc|cpp/utility/tuple/dsc swap2}}
 +
{{dsc end}}
  
{{dcl list begin}}
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
{{dcl list end}}
+

Latest revision as of 15:55, 4 January 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
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 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 */);
(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)

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 C++17)

Contents

[edit] Parameters

x, y - pairs whose contents to swap

[edit] Return value

(none)

[edit] Exceptions

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

[edit] Example

#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}

[edit] See also

swaps the values of two objects
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]