std::inplace_merge(3)
β¨ std::inplace_merge β¨
okay so this is the C++ Standard Libary manual page for std::inplace_merge(3) β yes bestie, βLibaryβ is literally how itβs spelled up in the header, and we are keeping it π
π NAME π
std::inplace_merge - std::inplace_merge
π οΈ Synopsis π οΈ
Defined in header <algorithm>
template< class BidirIt >
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last );
(1) (constexpr since C++26)
template< class ExecutionPolicy, class BidirIt >
void inplace_merge( ExecutionPolicy&& policy,
BidirIt first, BidirIt middle, BidirIt last );
(2) (since C++17)
template< class BidirIt, class Compare >
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last,
Compare comp );
(3) (constexpr 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).
1)If[first, middle)or[middle, last)is not sorted with respect tooperator<(until C++20) /std::less{}(since C++20), the behavior is undefined.3)If[first, middle)or[middle, last)is not sorted with respect tocomp, the behavior is undefined.2,4)Same as(1,3), but executed according topolicy.
These overloads participate in overload resolution only if
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true. (until C++20)
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true. (since C++20)
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). periodt π«
If any of the following conditions is satisfied, the behavior is undefined:
[first, middle)or[middle, last)is not a valid range.- The output range overlaps with
[first, middle)or[middle, last). - The type of
*firstis notSwappable.(until C++11) BiditItis notValueSwappable. (yes, the page saysBiditIthere β keeping it exactly, no cap)- The type of
*firstis notMoveConstructible.(since C++11) - The type of
*firstis notMoveAssignable.
πΈ Parameters πΈ
firstβ the beginning of the first sorted rangemiddleβ the end of the first sorted range and the beginning of the secondlastβ the end of the second sorted rangepolicyβ the execution policy to use. See execution policy for details.compβ comparison function object (i.e. an object that satisfies the requirements ofCompare) which returnstrueif 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:
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.
π¦ Type requirements π¦
BidirItmust meet the requirements ofLegacyBidirectionalIterator.Comparemust meet the requirements ofCompare.
π₯ Complexity π₯
Given N as std::distance(first, last):
- Exactly
N-1comparisons usingoperator<(until C++20) /std::less{}(since C++20) if enough additional memory is available,O(N Β· log(N))comparisons otherwise. O(N Β· log(N))comparisons usingoperator<(until C++20) /std::less{}(since C++20).- Exactly
N-1applications of the comparison functioncompif enough additional memory is available,O(N Β· log(N))applications otherwise. O(N Β· log(N))applications of the comparison functioncomp.
π Exceptions π
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
ExecutionPolicyis one of the standard policies,std::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_allocis thrown.
π Possible implementation π
See the implementations in libstdc++ and libc++.
π Notes π
This function attempts to allocate a temporary buffer. If the allocation fails, the less efficient algorithm is chosen.
__cpp_lib_constexpr_algorithmsβ value202306L, std(C++26), feature:constexpr inplace merging
π Example π
The following code is an implementation of merge sort. Itβs giving sorting-era main character energy β¨
// Run this code
#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 β
-2 0 1 2 3 7 8 11 11
π See also π
mergeβ merges two sorted ranges(function template)sortβ sorts a range into ascending order(function template)stable_sortβ sorts a range of elements while preserving order between equal elements(function template)ranges::inplace_merge(C++20)β merges two ordered ranges in-place(niebloid)
xoxo, source http://cppreference.com, version/date 2024.06.10, page std::inplace_merge(3) π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms