Defined in header <algorithm> | ||||
---|---|---|---|---|
|
Exchanges elements between range [first1, last1)
and another range starting at first2
.
Parameters
first1, last1 | - | the first range of elements to swap |
first2 | - | beginning of the second range of elements to swap |
Type requirements | ||
- ForwardIt1, ForwardIt2 must meet the requirements of ForwardIterator . | ||
-The types of dereferenced ForwardIt1 and ForwardIt2 must meet the requirements of Swappable |
Return value
Iterator to the element past the last element exchanged in the range beginning with first2
.
Possible implementation
|
Example
Demonstrates swapping of subranges from different containers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <algorithm> #include <list> #include <vector> #include <iostream> int main() { std::vector< int > v = {1, 2, 3, 4, 5}; std::list< int > l = {-1, -2, -3, -4, -5}; std::swap_ranges(v.begin(), v.begin()+3, l.begin()); for ( int n : v) std::cout << n << ' ' ; std::cout << '\n' ; for ( int n : l) std::cout << n << ' ' ; std::cout << '\n' ; } |
Output:
1 2 | -1 -2 -3 4 5 1 2 3 -4 -5 |
Complexity
linear in the distance between first
and last
.
See also
swaps the elements pointed to by two iterators (function template) | |
swaps the values of two objects (function template) | |
std::experimental::parallel::swap_ranges
(parallelism TS) | parallelized version of std::swap_ranges (function template) |
Please login to continue.