std::swap_ranges (3) - Linux Manuals
std::swap_ranges: std::swap_ranges
NAME
std::swap_ranges - std::swap_ranges
Synopsis
Defined in header <algorithm>
template< class ForwardIt1, class ForwardIt2 >
ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1, (until C++20)
ForwardIt2 first2 );
template< class ForwardIt1, class ForwardIt2 >
constexpr ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1, (1) (since C++20)
ForwardIt2 first2 );
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 swap_ranges( ExecutionPolicy&& policy, (2) (since C++17)
ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );
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
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
-
ForwardIt1, ForwardIt2 must meet the requirements of LegacyForwardIterator.
-
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.
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
Example
Demonstrates swapping of subranges from different containers
// Run this code
Output:
Complexity
linear in the distance between first1 and last1
See also
iter_swap (function template)
swap (function template)