Namespaces
Variants
Views
Actions

std::swap_ranges

From cppreference.com
< cpp‎ | algorithm
Revision as of 23:49, 18 January 2021 by Space Mission (Talk | contribs)

 
 
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>
(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.
Precondition: the two ranges [first1, last1) and [first2, last2) do not overlap, where last2 = std::next(first2, std::distance(first1, last1)).
2) Same as (1), but executed according to policy. This overload participates in overload resolution only if

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.

(until C++20)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.

(since C++20)

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
-
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.

Complexity

linear in the distance between first1 and last1.

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>
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 <list>
#include <vector>
#include <iostream>
#include <algorithm>
 
auto print = [](auto comment, auto const& seq) {
    std::cout << comment;
    for (const auto& e : seq) { std::cout << e << ' '; }
    std::cout << '\n';
};
 
int main()
{
    std::vector<char> v = {'a', 'b', 'c', 'd', 'e'};
    std::list<char> l = {'1', '2', '3', '4', '5'};
 
    print("Before swap_ranges:\n" "v: ", v);
    print("l: ", l);
 
    std::swap_ranges(v.begin(), v.begin()+3, l.begin());
 
    print("After swap_ranges:\n" "v: ", v);
    print("l: ", l);
}

Output:

Before swap_ranges:
v: a b c d e 
l: 1 2 3 4 5 
After swap_ranges:
v: 1 2 3 d e 
l: a b c 4 5

See also

swaps the elements pointed to by two iterators
(function template) [edit]
swaps the values of two objects
(function template) [edit]
swaps two ranges of elements
(niebloid)[edit]