Difference between revisions of "cpp/algorithm/swap"
From cppreference.com
(use templates) |
m (Use since= and until= params of {{dcl}} template.) |
||
Line 7: | Line 7: | ||
void swap( T& a, T& b ); | void swap( T& a, T& b ); | ||
}} | }} | ||
− | {{dcl | num=2 | | + | {{dcl | num=2 | since=c++11 | |
template< class T2, size_t N > | template< class T2, size_t N > | ||
void swap( T2 (&a)[N], T2 (&b)[N]); | void swap( T2 (&a)[N], T2 (&b)[N]); |
Revision as of 15:31, 1 July 2013
(until C++11) (since C++11) |
||
template< class T > void swap( T& a, T& b ); |
(1) | |
template< class T2, size_t N > void swap( T2 (&a)[N], T2 (&b)[N]); |
(2) | (since C++11) |
Exchanges the given values.
1) Swaps the values a
and b
.
2) Swaps the arrays a
and b
. In effect calls std::swap_ranges(a, a+N, b).
Contents |
Parameters
a, b | - | the values to be swapped |
Type requirements |
Return value
(none)
Exceptions
1)noexcept specification:
2) noexcept(noexcept(
std::is_nothrow_move_constructible<T>::value &&
std::is_nothrow_move_assignable<T>::value
noexcept specification:
noexcept(noexcept(swap(*a, *b)))
Complexity
1) Constant
2) Linear in N
Specializations
Both custom specializations and overloads of the std::swap algorithm are allowed, but the overloads are generally preferred since specializations of a function template aren't allowed for template classes. Library functions always use the user-provided overloads when swapping, if they are found by argument-dependent lookup (as per Template:concept concept).
The following overloads are already provided by the standard library:
(C++11) |
specializes the std::swap algorithm (function template) |
(C++11) |
specializes the std::swap algorithm (function template) |
(C++11) |
specializes the std::swap algorithm (function template) |
(C++11) |
specializes the std::swap algorithm (function template) |
(C++11) |
specializes the std::swap algorithm (function template) |
(C++11) |
specializes the std::swap algorithm (function template) |
specializes the std::swap algorithm (function template) | |
(C++11) |
specializes the std::swap algorithm (function template) |
specializes the std::swap algorithm (function template) | |
(C++11) |
specializes the std::swap algorithm (function template) |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
(C++11) |
specializes the std::swap algorithm (function template) |
specializes the std::swap algorithm (function template) | |
(C++11) |
specializes the std::swap algorithm (function template) |
(C++11) |
specializes the std::swap algorithm (function template) |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
specializes the std::swap algorithm (function template) | |
(C++11) |
specializes the std::swap algorithm (function template) |
specializes the std::swap algorithm (function template) | |
({{{1}}}) |
specializes the std::swap algorithm (function) |
(C++11) |
specializes the std::swap algorithm (function template) |
(C++11) |
specializes the std::swap algorithm (function template) |
specializes the std::swap algorithm (function template) |
Example
Run this code
Output:
5 3 3 5
See also
swaps the elements pointed to by two iterators (function template) | |
swaps two ranges of elements (function template) |