std::inplace_merge
| Defined in header <algorithm>
|
||
template< class BidirIt >
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last );
|
(1) | (constexpr since C++26) |
template< class BidirIt, class Compare >
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last,
Compare comp );
|
(2) | (constexpr since C++26) |
template< class ExecutionPolicy, class BidirIt >
void inplace_merge( ExecutionPolicy&& policy,
BidirIt first, BidirIt middle, BidirIt last );
|
(3) | (since C++17) |
template< class ExecutionPolicy, class BidirIt, class Compare >
void inplace_merge( ExecutionPolicy&& policy,
BidirIt first, BidirIt middle, BidirIt last,
Compare comp );
|
(4) | (since C++17) |
Makes the target range [first, last) sorted by merging its two consecutive sorted subranges [first, middle) and [middle, last).
For each group of equivalent elements in the sorted target range, any pair of elements from the group has the following order:
- If the two elements are from the same subrange, the order between them in the target range follows the original order between them in the subrange.
- Otherwise, the element from the first subrange is always placed before the element from the second subrange.
operator<(until C++20)std::less{}(since C++20).operator<(until C++20)std::less{}(since C++20), the behavior is undefined.comp.comp, the behavior is undefined.policy.true:
|
|
(until C++20) |
|
|
(since C++20) |
If any of the following conditions is satisfied, the behavior is undefined:
|
(until C++11) |
|
(since C++11) |
Parameters
| first | - | the beginning of the first subrange |
| middle | - | the end of the first subrange and the beginning of the second |
| last | - | the end of the second subrange |
| comp | - | comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than (i.e. is ordered before) the second. The signature of the comparison function should be equivalent to the following:
While the signature does not need to have |
| policy | - | the execution policy to use |
| Type requirements | ||
-BidirIt must meet the requirements of LegacyBidirectionalIterator.
| ||
-Compare must meet the requirements of Compare.
| ||
Complexity
Given N as std::distance(first, last):
operator<(until C++20)std::less{}(since C++20) (or only exactly N-1 comparisons if enough additional memory is available).comp (or only exactly N-1 applications if enough additional memory is available).operator<(until C++20)std::less{}(since C++20).comp.Exceptions
- If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
- If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).
Notes
This function attempts to allocate a temporary buffer. If the allocation fails, the less efficient algorithm is chosen.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_constexpr_algorithms |
202306L |
(C++26) | constexpr inplace merging (1), (2)
|
Possible implementation
See the implementations in libstdc++ and libc++.
Example
The following code is an implementation of merge sort.
#include <array>
#include <algorithm>
#include <print>
template<class Iter>
void merge_sort(Iter first, Iter last)
{
if (last - first > 1)
{
Iter middle = first + (last - first) / 2;
merge_sort(first, middle);
merge_sort(middle, last);
std::inplace_merge(first, middle, last);
}
}
int main()
{
std::array v{8, 2, -2, 0, 11, 11, 1, 7, 3};
std::print("{}\n", v);
merge_sort(v.begin(), v.end());
std::print("{}\n", v);
}
Output:
[8, 2, -2, 0, 11, 11, 1, 7, 3]
[-2, 0, 1, 2, 3, 7, 8, 11, 11]
See also
(C++20) |
merges two ordered ranges in-place (algorithm function object) |
| merges two sorted ranges (function template & algorithm function object) | |
(C++20) |
|
| sorts a range of elements (function template & algorithm function object) | |
(C++20) |
|
| sorts a range of elements while preserving relative order between equivalent elements (function template & algorithm function object) | |
(C++20) |