Skip to main content

std::swap_ranges() algorithm

// (1)
template< class ForwardIt1, class ForwardIt2 >
constexpr ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );

// (2)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 swap_ranges( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );
  • (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.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (until C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (since C++20) is true.

Parameters

first1
last1

The first range to swap.

first2

The beginning of the second range to swap.

policy

The execution policy to use. See execution policy for details.

Type requirements

ForwardIt1
ForwardIt2
LegacyForwardIterator

The types of dereferenced ForwardIt1 and ForwardIt2 must meet the requirements of Swappabler.

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 overloads with a template parameter named ExecutionPolicy report 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

swap_ranges (1)

template<class ForwardIt1, class ForwardIt2>
constexpr ForwardIt2 swap_ranges(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2)
{
for (; first1 != last1; ++first1, ++first2)
std::iter_swap(first1, first2);

return first2;
}

Notes

Implementations (e.g. MSVC STL) may enable vectorization w hen the iterator type satisfies LegacyContiguousIterator and swapping its value type calls neither non-trivial special member function nor ADL-found swap.

Examples

Main.cpp
#include <algorithm>
#include <iostream>
#include <list>
#include <vector>

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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::swap_ranges() algorithm

// (1)
template< class ForwardIt1, class ForwardIt2 >
constexpr ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );

// (2)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 swap_ranges( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );
  • (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.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (until C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (since C++20) is true.

Parameters

first1
last1

The first range to swap.

first2

The beginning of the second range to swap.

policy

The execution policy to use. See execution policy for details.

Type requirements

ForwardIt1
ForwardIt2
LegacyForwardIterator

The types of dereferenced ForwardIt1 and ForwardIt2 must meet the requirements of Swappabler.

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 overloads with a template parameter named ExecutionPolicy report 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

swap_ranges (1)

template<class ForwardIt1, class ForwardIt2>
constexpr ForwardIt2 swap_ranges(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2)
{
for (; first1 != last1; ++first1, ++first2)
std::iter_swap(first1, first2);

return first2;
}

Notes

Implementations (e.g. MSVC STL) may enable vectorization w hen the iterator type satisfies LegacyContiguousIterator and swapping its value type calls neither non-trivial special member function nor ADL-found swap.

Examples

Main.cpp
#include <algorithm>
#include <iostream>
#include <list>
#include <vector>

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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.