template< class BidirIt >
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last );
void inplace_merge( ExecutionPolicy&& policy,
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last,
void inplace_merge( ExecutionPolicy&& policy,
BidirIt first, BidirIt middle, BidirIt last,
Merges two consecutive sorted ranges [
first,
middle)
and [
middle,
last)
into one sorted range [
first,
last)
.
If
[
first,
middle)
or
[
middle,
last)
is not
sortedwith respect to
operator<(until C++20)std::less{}(since C++20), the behavior is undefined.
3) If [
first,
middle)
or [
middle,
last)
is not sorted with respect to comp, the behavior is undefined.
2,4) Same as (1,3), but executed according to policy.
These overloads participate in overload resolution only if all following conditions are satisfied:
This merge function 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).
If any of the following conditions is satisfied, the behavior is undefined:
[
first,
middle)
or [
middle,
last)
is not a valid range.The signature of the comparison function should be equivalent to the following:
bool cmp(const Type1& a, const Type2& b);
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, nor is Type1
unless for Type1
a move is equivalent to a copy(since C++11)).
The types Type1 and Type2 must be such that an object of type BidirIt can be dereferenced and then implicitly converted to both of them. â
BidirIt
must meet the requirements of LegacyBidirectionalIterator. -Compare
must meet the requirements of Compare. [edit] Complexity
Given \(\scriptsize N\)N as std::distance(first, last):
1)Exactly
\(\scriptsize N-1\)N-1comparisons using
operator<(until C++20)std::less{}(since C++20)if enough additional memory is available,
\(\scriptsize O(N \cdot \log(N))\)O(Nâ log(N))comparisons otherwise.
2) \(\scriptsize O(N \cdot \log(N))\)O(Nâ log(N))comparisons using
operator<(until C++20)std::less{}(since C++20).
3) Exactly \(\scriptsize N-1\)N-1 applications of the comparison function comp if enough additional memory is available, \(\scriptsize O(N \cdot \log(N))\)O(Nâ log(N)) applications otherwise.
4) \(\scriptsize O(N \cdot \log(N))\)O(Nâ log(N)) applications of the comparison function comp.
[edit] ExceptionsThe 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.See the implementations in libstdc++ and libc++.
[edit] NotesThis function attempts to allocate a temporary buffer. If the allocation fails, the less efficient algorithm is chosen.
[edit] ExampleThe 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 (const auto& n : v) std::cout << n << ' '; std::cout << '\n'; }
Output:
[edit] See also merges two sorted rangesRetroSearch 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