| |
|
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 ForwardIterator. 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.
Complexity
Linear. Exactly last1 - first1
swaps are performed.
Example
Vector<int> V1, V2;
V1.push_back(1);
V1.push_back(2);
V2.push_back(3);
V2.push_back(4);
assert(V1[0] == 1 && V1[1] == 2 && V2[0] == 3 && V2[1] == 4);
swap_ranges(V1.begin(), V1.end(), V2.begin());
assert(V1[0] == 3 && V1[1] == 4 && V2[0] == 1 && V2[1] == 2);
Notes
See also
swap
, iter_swap
.