function template
<utility>
std::swap (pair)
template <class T1, class T2> void swap (pair<T1,T2>& x, pair<T1,T2>& y) noexcept (noexcept(x.swap(y)));
Exchanges the contents of two pairs
The contents of container x are exchanged with those of y. Both container objects shall be of the same type (same template parameters).
This is an overload of the generic swap that improves its performance by calling pair::swap as in: x.swap(y)
.
Parameters
- x,y
- pair objects of the same type (i.e., having both the same template parameters, T1 and T2).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// swap (pair overload)
#include <utility> // std::pair
#include <iostream> // std::cout
int main () {
std::pair<int,char> foo (10,'a');
std::pair<int,char> bar (90,'z');
swap (foo,bar);
std::cout << "foo contains: " << foo.first;
std::cout << " and " << foo.second << '\n';
return 0;
}
|
Output:
Data races
The members of both pair objects, x and y, are modified.
Exception safety
If both T1 and T2 have a swap function defined and this is noexcept, this 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 x and y end up in a valid state in case of exception (basic guarantee).
See also
- pair::swap
- Swap contents (public member function)
- swap
- Exchange values of two objects (function template)