public member function
<utility>

std::pair::swap

void swap (pair& pr) noexcept ( noexcept(swap(first,pr.first)) &&                                noexcept(swap(second,pr.second)) );
Swap contents
Exchanges the contents of the pair object with the contents of pr.

The function calls swap (unqualified) to exchange the elements.

It is defined with the same behavior as:
1
2
3
4
5
void swap (pair& pr) noexcept ( noexcept(swap(first,pr.first)) &&
                                noexcept(swap(second,pr.second)) ) {
  swap(first,pr.first);
  swap(second,pr.second);
}

Parameters

pr
Another pair object of the same type (i.e., with the same class template parameters, T1 and T2).

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// pair::swap example
#include <utility>      // std::pair
#include <iostream>     // std::cout

int main () {
  std::pair<int,char> foo (10,'a');
  std::pair<int,char> bar (90,'z');

  foo.swap(bar);

  std::cout << "foo contains: " << foo.first;
  std::cout << " and " << foo.second << '\n';

  return 0;
}

Output:
foo contains: 90 and z


Data races

The members of both pair objects are modified.

Exception safety

If both the types of first and second have a swap function defined and this is noexcept, this member function never throws exceptions (no-throw guarantee).
Otherwise, if at least one of them swaps with move semantics, the operation may leave either or both pair objects in an invalid state in case of exception (no guarantees).
Otherwise, the operation guarantees that both pair objects involved end up in a valid state in case of exception (basic guarantee).

See also