Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/swap ranges"

From cppreference.com
< cpp‎ | algorithm
m (seealso par)
(+)
Line 3: Line 3:
 
{{dcl begin}}
 
{{dcl begin}}
 
{{dcl header | algorithm}}
 
{{dcl header | algorithm}}
{{dcl |
+
{{dcl | since= | num= 1 |1=
 
template< class ForwardIt1, class ForwardIt2 >
 
template< class ForwardIt1, class ForwardIt2 >
 
ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );
 
ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );
 
}}
 
}}
 +
{{dcl | since=c++17 | num= 2 |1=
 +
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
 +
ForwardIt2 swap_ranges( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );}}
 
{{dcl end}}
 
{{dcl end}}
  
Exchanges elements between range {{tt|[first1, last1)}} and another range starting at {{tt|first2}}.  
+
@1@ Exchanges elements between range {{tt|[first1, last1)}} and another range starting at {{tt|first2}}.  
 +
 
 +
@2@ Same as {{v|1}}, but executed according to {{tt|policy}}. This overload does not participate in overload resolution unless {{c|std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>}} is true
  
 
===Parameters===
 
===Parameters===
Line 15: Line 20:
 
{{par | first1, last1 | the first range of elements to swap}}
 
{{par | first1, last1 | the first range of elements to swap}}
 
{{par | first2 | beginning of the second range of elements to swap}}
 
{{par | first2 | beginning of the second range of elements to swap}}
 +
{{par exec pol}}
 
{{par hreq}}
 
{{par hreq}}
 
{{par req concept | ForwardIt1, ForwardIt2 | ForwardIterator}}
 
{{par req concept | ForwardIt1, ForwardIt2 | ForwardIterator}}
Line 22: Line 28:
 
===Return value===
 
===Return value===
 
Iterator to the element past the last element exchanged in the range beginning with {{tt|first2}}.
 
Iterator to the element past the last element exchanged in the range beginning with {{tt|first2}}.
 +
 +
===Exceptions===
 +
{{cpp/algorithm/parallel_exceptions_reporting_behavior|singular=yes}}
  
 
===Possible implementation===
 
===Possible implementation===

Revision as of 09:07, 21 March 2016

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
swap_ranges

Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
Defined in header <algorithm>
template< class ForwardIt1, class ForwardIt2 >
ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );
(1)
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 true

Contents

Parameters

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

Template:par req concept

-
The types of dereferenced ForwardIt1 and ForwardIt2 must meet the requirements of Template:concept

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 ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

template<class ForwardIt1, class ForwardIt2>
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 first and last

See also

swaps the elements pointed to by two iterators
(function template) [edit]
swaps the values of two objects
(function template) [edit]
parallelized version of std::swap_ranges
(function template) [edit]