A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/algorithm/../ranges/../algorithm/swap_ranges.html below:

std::swap_ranges - cppreference.com

template< class ForwardIt1, class ForwardIt2 >

ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1,

                        ForwardIt2 first2 );
(1) (constexpr 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

[first1last1)

and another range of

std::distance(first1, last1)

elements starting at

first2

.

2) Same as (1), but executed according to policy.

This overload participates in overload resolution only if all following conditions are satisfied:

If any of the following conditions is satisfied, the behavior is undefined:

[edit] Parameters first1, last1 - the pair of iterators defining the range of elements to swap first2 - beginning of the second range of elements to swap policy - the execution policy to use Type requirements -ForwardIt1, ForwardIt2 must meet the requirements of LegacyForwardIterator. [edit] Return value

Iterator to the element past the last element exchanged in the range beginning with first2.

[edit] Complexity

Exactly std::distance(first1, last1) swaps.

[edit] Exceptions

The overload with a template parameter named ExecutionPolicy reports errors as follows:

[edit] Notes

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

[edit] Possible implementation
template<class ForwardIt1, class ForwardIt2>
constexpr //< since C++20
ForwardIt2 swap_ranges(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2)
{
    for (; first1 != last1; ++first1, ++first2)
        std::iter_swap(first1, first2);
 
    return first2;
}
[edit] Example

Demonstrates swapping of subranges from different containers.

#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
[edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4