Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.
Requirements on types • Assignable is a model of Assignable.
Preconditions None.
Complexity Amortized constant time. [1] [2]
Example int x = 1;
int y = 2;
assert(x == 1 && y == 2);
swap(x, y);
assert(x == 2 && y == 1);
Notes [1] The time required to swap two objects of type T will obviously depend on the type; 'constant time' does not mean that performance will be the same for an 8-bit char as for a 128-bit complex<double>.
[2] This implementation of swap makes one call to a copy constructor and two calls to an assignment operator; roughly, then, it should be expected to take about the same amount of time as three assignments. In many cases, however, it is possible to write a specialized version of swap that is far more efficient. Consider, for example, swapping two vector<double>s each of which has N elements. The unspecialized version requires 3*N assignments of double, but a specialized version requires only nine pointer assignments. This is important because swap is used as a primitive operation in many other STL algorithms, and because containers of containers (list<vector<char> >, for example) are very common. The STL includes specialized versions of swap for all container classes. User-defined types should also provide specialized versions of swap whenever it is possible to write one that is more efficient than the general version.
See also iter_swap, swap_ranges
Category: algorithms
Component type: function
Prototype template <class ForwardIterator1, class ForwardIterator 2>
inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b);
Description Equivalent to swap(*a, *b). [1]
Definition Declared in algo.h. The implementation is in algobase.h.
Requirements on types • ForwardIterator1 and ForwardIterator2 are models of Forward Iterator.
• ForwardIterator1 and ForwardIterator2 are mutable.
• ForwardIterator1 and ForwardIterator2 have the same value type.
Preconditions • ForwardIterator1 and ForwardIterator2 are dereferenceable.
Complexity See swap for a discussion.
Example int x = 1;
int y = 2;
assert(x == 1 && y == 2);
iter_swap(&x, &y);
assert(x == 2 && y == 1);
Notes [1] Strictly speaking, iter_swap is redundant. It exists only for technical reasons: in some circumstances, some compilers have difficulty performing the type deduction required to interpret swap(*a, *b).
See also swap, swap_ranges
Category: algorithms
Component type: function
Prototype template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
Description Swap_ranges swaps each of the elements in the range [first1, last1) with the corresponding element in the range [first2, first2 + (last1 – first1)) . That is, for each integer n such that 0 <= n < (last1 – first1), it swaps *(first1 + n) and *(first2 + n). The return value is first2 + (last1 – first1).
Definition Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.
Requirements on types ForwardIterator1 and ForwardIterator2 must both be models of Forward Iterator. The value types of ForwardIterator1 and ForwardIterator2 must be convertible to each other.
Preconditions • [first1, last1) is a valid range.
• [first2, first2 + (last1 – first1)) is a valid range.
• The two ranges [first1, last1) and [first2, first2 + (last1 – first1)) do not overlap.