std::swap_ranges
| Defined in header <algorithm> | ||
|---|---|---|
| (1) | ||
| template< class ForwardIt1, class ForwardIt2 >
ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1,
                        ForwardIt2 first2 ); | (until C++20) | |
| template< class ForwardIt1, class ForwardIt2 >
constexpr ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1,
                                  ForwardIt2 first2 ); | (since C++20) | |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 swap_ranges( ExecutionPolicy&& policy, 
                        ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 ); | (2) | (since C++17) | 
1) Exchanges elements between range 
 [first1, last1) and another range starting at first2. 
2) Same as (1), but executed according to 
 policy. This overload does not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is trueParameters
| first1, last1 | - | the first range of elements to swap | 
| first2 | - | beginning of the second range of elements to swap | 
| policy | - | the execution policy to use. See execution policy for details. | 
| Type requirements | ||
| - ForwardIt1, ForwardIt2must meet the requirements of LegacyForwardIterator. | ||
| -The types of dereferenced ForwardIt1andForwardIt2must meet the requirements of Swappable | ||
Return value
Iterator to the element past the last element exchanged in the range beginning with first2.
Exceptions
The overload with a template parameter named ExecutionPolicy reports errors as follows:
-  If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicyis one of the standard policies,std::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined.
-  If the algorithm fails to allocate memory, std::bad_allocis thrown.
Possible implementation
| template<class ForwardIt1, class ForwardIt2>
constexpr ForwardIt2 swap_ranges(ForwardIt1 first1, 
                             ForwardIt1 last1, 
                             ForwardIt2 first2)
{
    while (first1 != last1) {
        std::iter_swap(first1++, first2++);
    }
    return first2;
} | 
Example
Demonstrates swapping of subranges from different containers.
#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 -3 4 5 1 2 3 -4 -5
Complexity
linear in the distance between first1 and last1.
See also
| swaps the elements pointed to by two iterators (function template) | |
| swaps the values of two objects (function template) | 
    © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
    http://en.cppreference.com/w/cpp/algorithm/swap_ranges