W3cubDocs

/C++

std::pair::swap

void swap(pair& other);
(since C++11)

Swaps first with other.first and second with other.second.

This function does not participate in overload resolution unless std::is_swappable_v<first_type> && std::is_swappable_v<second_type>

(since C++17)

Parameters

other - pair of values to swap

Return value

(none).

Exceptions

Example

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

Output:

(10, test)

See also

noexcept specification:
noexcept(

noexcept(std::swap(first, other.first)) &&
noexcept(std::swap(second, other.second))
.

)
(until C++17)
noexcept specification:
noexcept(

std::is_nothrow_swappable<first_type>::value) &&
std::is_nothrow_swappable<second_type>::value)
.

)
(since C++17)
swaps the values of two objects
(function template)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/utility/pair/swap