Movatterモバイル変換


[0]ホーム

URL:


cplusplus.com

Reference

<algorithm>

function template
C++98: <algorithm>, C++11: <utility>

std::swap

template <class T> void swap (T& a, T& b);
header
// moved from <algorithm> to <utility> in C++11
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
Exchanges the values ofa andb.

The behavior of this function template is equivalent to:
1
2
3
4
template <class T>void swap ( T& a, T& b ){  T c(a); a=b; b=c;}

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, allstandard 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.
This function is no longer defined in header<algorithm>, but in<utility>.

The behavior of these function templates is equivalent to:
1
2
3
4
5
6
7
8
template <class T>void swap (T& a, T& b){  T c(std::move(a)); a=std::move(b); b=std::move(c);}template <class T, size_t N>void swap (T (&a)[N], T (&b)[N]){for (size_t i = 0; i<N; ++i) swap (a[i],b[i]);}

Many components of the standard library (withinstd) callswap in anunqualified manner to allow custom overloads for non-fundamental types to be called instead of this generic version: Custom overloads ofswap declared in the same namespace as the type for which they are provided get selected throughargument-dependent lookup over this generic version.

Parameters

a, b
Two objects, whose contents are swapped.
TypeT shall becopy-constructible andassignable.
TypeT shall bemove-constructible andmove-assignable (or haveswap defined for it, for version(2)).

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// swap algorithm example (C++98)#include <iostream>// std::cout#include <algorithm>// std::swap#include <vector>// std::vectorint main () {int x=10, y=20;// x:10 y:20  std::swap(x,y);// x:20 y:10  std::vector<int> foo (4,x), bar (6,y);// foo:4x20 bar:6x10  std::swap(foo,bar);// foo:6x10 bar:4x20  std::cout <<"foo contains:";for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)    std::cout <<' ' << *it;  std::cout <<'\n';return 0;}

Output:
foo contains: 10 10 10 10 10 10


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 inN: performs a swap operation per element.

Data races

Botha andb are modified.

Exceptions

Throws if the construction or assignment of typeT throws.
Never throws ifT isnothrow-move-constructible andnothrow-move-assignable.
Note that ifT does not fulfill the requirements specified above (inparameters), it causesundefined 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

[8]ページ先頭

©2009-2026 Movatter.jp