CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 23 Jul 2025 04:07:12 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/utility/swap/
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 23 Jul 2025 04:07:12 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"7389-O45EspESnjW2fMD2vaUg7n6hlb4"
Content-Encoding: gzip
Exchanges the values of a and b.
Many components of the standard library (within std) call swap in an unqualified manner to allow custom overloads for non-fundamental types to be called instead of this generic version: Custom overloads of swap declared in the same namespace as the type for which they are provided get selected through argument-dependent lookup over this generic version.
Output:
Array: Linear in N: performs a swap operation per element.
Never throws if T is nothrow-move-constructible and nothrow-move-assignable.
Note that if T does not fulfill the requirements specified above (in parameters), it causes undefined behavior.
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
<utility>
classes
functions
types
-
piecewise_construct_tC++11
-
constants
-
piecewise_constructC++11
-
namespaces
function template
C++98: <algorithm>, C++11: <utility>
std::swap
// defined in <algorithm> before C++11template <class T> void swap (T& a, T& b);
non-array (1) | template <class T> void swap (T& a, T& b) noexcept (is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value); |
---|---|
array (2) | template <class T, size_t N> void swap(T (&a)[N], T (&b)[N]) noexcept (noexcept(swap(*a,*b))); |
Exchange values of two objects
Before C++11, this function was defined in header
The behavior of this function template is equivalent to:
Notice how this function involves a copy construction and two assignment operations, which may not be the most efficient way of swapping the contents of classes that store large quantities of data, since each of these operations generally operate in linear time on their size.
Large data types can provide an overloaded version of this function optimizing its performance. Notably, all standard containers specialize it in such a way that only a few internal pointers are swapped instead of their entire contents, making them operate in constant time.
<algorithm>
.The behavior of this function template is equivalent to:
|
|
Notice how this function involves a copy construction and two assignment operations, which may not be the most efficient way of swapping the contents of classes that store large quantities of data, since each of these operations generally operate in linear time on their size.
Large data types can provide an overloaded version of this function optimizing its performance. Notably, all standard containers specialize it in such a way that only a few internal pointers are swapped instead of their entire contents, making them operate in constant time.
The behavior of these function templates is equivalent to:
|
|
Many components of the standard library (within std) call swap in an unqualified manner to allow custom overloads for non-fundamental types to be called instead of this generic version: Custom overloads of swap declared in the same namespace as the type for which they are provided get selected through argument-dependent lookup over this generic version.
Parameters
- a, b
- Two objects, whose contents are swapped.
Type T shall be copy-constructible and assignable.
Type T shall be move-constructible and move-assignable (or have swap defined for it, for version (2)).
Return value
noneExample
|
|
Output:
foo contains: 10 20 30 40 |
Complexity
Non-array: Constant: Performs exactly one construction and two assignments (although notice that each of these operations works on its own complexity).Array: Linear in N: performs a swap operation per element.
Data races
Both a and b are modified.Exceptions
Throws if the construction or assignment of type T throws.Never throws if T is nothrow-move-constructible and nothrow-move-assignable.
Note that if T does not fulfill the requirements specified above (in parameters), it causes undefined behavior.
See also
- copy
- Copy range of elements (function template)
- fill
- Fill range with value (function template)
- replace
- Replace value in range (function template)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us