Constrained algorithms and algorithms on ranges(C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Constrained algorithms, e.g.ranges::copy,ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Execution policies(C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations on uninitialized memory | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Defined in header <algorithm> | ||
template<class ForwardIt1,class ForwardIt2> ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1, | (1) | (constexpr since C++20) |
template<class ExecutionPolicy, class ForwardIt1,class ForwardIt2> | (2) | (since C++17) |
[
first1,
last1)
and another range ofstd::distance(first1, last1) elements starting atfirst2.std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> istrue. | (until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue. | (since C++20) |
If any of the following conditions is satisfied, the behavior is undefined:
Contents |
first1, last1 | - | the pair of iterators defining therange of elements to swap |
first2 | - | beginning of the second range of elements to swap |
policy | - | theexecution policy to use |
Type requirements | ||
-ForwardIt1, ForwardIt2 must meet the requirements ofLegacyForwardIterator. |
Iterator to the element past the last element exchanged in the range beginning withfirst2.
Exactlystd::distance(first1, last1) swaps.
The overload with a template parameter namedExecutionPolicy
reports errors as follows:
ExecutionPolicy
is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy
, the behavior is implementation-defined.Implementations (e.g.MSVC STL) may enable vectorization when the iterator type satisfiesLegacyContiguousIterator and swapping its value type calls neither non-trivial special member function norADL-foundswap
.
template<class ForwardIt1,class ForwardIt2>constexpr//< since C++20ForwardIt2 swap_ranges(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2){for(; first1!= last1;++first1,++first2)std::iter_swap(first1, first2); return first2;} |
Demonstrates swapping of subranges from different containers.
#include <algorithm>#include <iostream>#include <list>#include <vector> auto print=[](auto comment,autoconst& seq){std::cout<< comment;for(constauto& 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 el: 1 2 3 4 5After swap_ranges:v: 1 2 3 d el: a b c 4 5
swaps the elements pointed to by two iterators (function template)[edit] | |
swaps the values of two objects (function template)[edit] | |
(C++20) | swaps two ranges of elements (algorithm function object)[edit] |