Defined in header <algorithm> | ||
---|---|---|
(1) | ||
template< class BidirIt > void inplace_merge( BidirIt first, BidirIt middle, BidirIt last ); | (until C++26) | |
template< class BidirIt > constexpr void inplace_merge( BidirIt first, BidirIt middle, BidirIt last ); | (since C++26) | |
template< class ExecutionPolicy, class BidirIt > void inplace_merge( ExecutionPolicy&& policy, BidirIt first, BidirIt middle, BidirIt last ); | (2) | (since C++17) |
(3) | ||
template< class BidirIt, class Compare > void inplace_merge( BidirIt first, BidirIt middle, BidirIt last, Compare comp ); | (until C++26) | |
template< class BidirIt, class Compare > constexpr void inplace_merge( BidirIt first, BidirIt middle, BidirIt last, Compare comp ); | (since C++26) | |
template< class ExecutionPolicy, class BidirIt, class Compare > void inplace_merge( ExecutionPolicy&& policy, BidirIt first, BidirIt middle, BidirIt last, Compare comp ); | (4) | (since C++17) |
Merges two consecutive sorted ranges [
first
,
middle
)
and [
middle
,
last
)
into one sorted range [
first
,
last
)
.
A sequence [
first
,
last
)
is said to be sorted with respect to a comparator comp
if for any iterator it
pointing to the sequence and any non-negative integer n
such that it + n
is a valid iterator pointing to an element of the sequence, comp(*(it + n), *it)
evaluates to false
.
This merge is stable, which means that 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).
operator<
and the ranges must be sorted with respect to the same.comp
and the ranges must be sorted with respect to the same.policy
. These overloads do not participate in overload resolution unless
| (until C++20) |
| (since C++20) |
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 | - | 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 const&, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) |
Type requirements | ||
-BidirIt must meet the requirements of ValueSwappable and LegacyBidirectionalIterator. |
||
-The type of dereferenced BidirIt must meet the requirements of MoveAssignable and MoveConstructible. |
(none).
Given N = std::distance(first, last)
,
N - 1
comparisons if enough additional memory is available. If the memory is insufficient, O(N log N)
comparisons.O(N log N)
comparisons.The overloads with a template parameter named ExecutionPolicy
report errors as follows:
ExecutionPolicy
is one of the standard policies, std::terminate
is called. For any other ExecutionPolicy
, the behavior is implementation-defined. std::bad_alloc
is thrown. This function attempts to allocate a temporary buffer. If the allocation fails, the less efficient algorithm is chosen.
Feature-test macro | Value | Comment |
---|---|---|
__cpp_lib_constexpr_algorithms | 202306L |
constexpr stable sorting |
See the implementations in libstdc++ and libc++.
The following code is an implementation of merge sort.
#include <algorithm> #include <iostream> #include <vector> 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::vector<int> v {8, 2, -2, 0, 11, 11, 1, 7, 3}; merge_sort(v.begin(), v.end()); for (auto n : v) std::cout << n << ' '; std::cout << '\n'; }
Output:
-2 0 1 2 3 7 8 11 11
merges two sorted ranges (function template) |
|
sorts a range into ascending order (function template) |
|
sorts a range of elements while preserving order between equal elements (function template) |
|
(C++20) | merges two ordered ranges in-place (niebloid) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/inplace_merge