std::inplace_merge (3) - Linux Man Pages
Defined in header <algorithm>
template< class BidirIt > (1)
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last );
template< class ExecutionPolicy, class BidirIt > (2) (since C++17)
void inplace_merge( ExecutionPolicy&& policy, BidirIt first, BidirIt middle, BidirIt last );
template< class BidirIt, class Compare> (3)
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last, Compare comp );
template< class ExecutionPolicy, class BidirIt, class Compare> (4) (since C++17)
void inplace_merge( ExecutionPolicy&& policy, BidirIt first, BidirIt middle, BidirIt last, Compare comp );
Merges two consecutive sorted ranges [first, middle) and [middle, last) into one sorted range [first, last). For equivalent elements in the original two ranges, the elements from the first range (preserving their original order) precede the elements from the second range (preserving their original order).
1) Elements are compared using operator< and the ranges must be sorted with respect to the same.
3) Elements are compared using the given binary comparison function comp and the ranges must be sorted with respect to the same.
2,4) Same as (1,3), but executed according to policy. These overloads do not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true
first - the beginning of the first sorted range
middle - the end of the first sorted range and the beginning of the second
last - the end of the second sorted range
policy - the execution policy to use. See execution_policy for details.
comp - While the signature does not need to have const &, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value_category (thus, Type1 & is not allowed
BidirIt must meet the requirements of ValueSwappable and LegacyBidirectionalIterator.
The type of dereferenced BidirIt must meet the requirements of MoveAssignable and MoveConstructible.
Given N = std::distance(first, last)},
1,3) Exactly N-1 comparisons if enough additional memory is available. If the memory is insufficient, O(N log N) comparisons.
2,4) O(N log N) comparisons.
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.
This function attempts to allocate a temporary buffer. If the allocation fails, the less efficient algorithm is chosen.
The following code is an implementation of merge sort.
// Run this code
merge (function template)
sort (function template)
stable_sort (function template)