cpp/algorithm/stable partition:修订间差异
来自cppreference.com
小无编辑摘要 |
无编辑摘要 |
||
| 第4行: | 第4行: | ||
{{dcl header|algorithm}} | {{dcl header|algorithm}} | ||
{{dcl|num=1|notes={{mark constexpr since c++26}}| | {{dcl|num=1|notes={{mark constexpr since c++26}}| | ||
template< class BidirIt, class | template< class BidirIt, class > | ||
BidirIt stable_partition( BidirIt first, BidirIt last, | BidirIt stable_partition( BidirIt first, BidirIt last, p ); | ||
}} | }} | ||
{{dcl|since=c++17|num=2|1= | {{dcl|since=c++17|num=2|1= | ||
template< class ExecutionPolicy, class BidirIt, class | template< class ExecutionPolicy, class BidirIt, class > | ||
BidirIt stable_partition( ExecutionPolicy&& policy, | BidirIt stable_partition( ExecutionPolicy&& policy, | ||
BidirIt first, BidirIt last, | BidirIt first, BidirIt last, p ); | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
| 第16行: | 第16行: | ||
@1@ 重排序范围 {{range|first|last}} 中的元素,使得所有谓词 {{c|p}} 对其返回 {{c|true}} 的元素均先于谓词 {{c|p}} 对其返回 {{c|false}} 的元素。保持元素的相对顺序。 | @1@ 重排序范围 {{range|first|last}} 中的元素,使得所有谓词 {{c|p}} 对其返回 {{c|true}} 的元素均先于谓词 {{c|p}} 对其返回 {{c|false}} 的元素。保持元素的相对顺序。 | ||
@2@ 同 {{v|1}},但按照 {{c|policy}} 执行。{{cpp/algorithm/parallel overload precondition}} | @2@ 同 {{v|1}},但按照 {{c|policy}} 执行。 | ||
{{cpp/algorithm/parallel overload precondition | |||
}} | |||
===参数=== | ===参数=== | ||
{{par begin}} | {{par begin}} | ||
{{par | first, last |要重排序的元素范围}} | {{par|first, last|要重排序的元素范围}} | ||
{{par exec pol}} | {{par exec pol}} | ||
{{par pred1 | p | | {{par pred1|p|元素应其他元素|p1=BidirIt}} | ||
{{par hreq}} | {{par hreq}} | ||
{{par req named | BidirIt | BidirectionalIterator | {{par req named|BidirIt|BidirectionalIterator}} | ||
{{par req named||Predicate}} | |||
{{par req named | | |||
{{par end}} | {{par end}} | ||
===返回值=== | ===返回值=== | ||
指向第二个分组首元素的迭代器 | 指向第二个分组首元素的迭代器 | ||
===复杂度=== | ===复杂度=== | ||
给定 {{mathjax-or|\(\scriptsize N\)|N}} 为 {{c|std::distance(first, last)}}: | 给定 {{mathjax-or|\(\scriptsize N\)|N}} 为 {{c|std::distance(first, last)}}: | ||
@1@ | |||
@2@ {{mathjax-or|\(\scriptsize | @1@ 应用 {{mathjax-or|\(\scriptsize N\)|N}} 次 | ||
{{mathjax-or|\(\scriptsize O(N)\)|O(N)}} 次交换多 {{mathjax-or|\(\scriptsize N (N)\)|N(N)}} 次。 | |||
@2@ {{mathjax-or|\(\scriptsize (N)\)|(N)}} 次 | |||
交换 {{mathjax-or|\(\scriptsize (N)\)|(N)}} 次。 | |||
===异常=== | ===异常=== | ||
| 第41行: | 第57行: | ||
===注解=== | ===注解=== | ||
此函数试图分配临时缓冲区。 | 此函数试图分配临时缓冲区。分配失败,选择较低效的算法。 | ||
[https://github.com/llvm/llvm-project/blob/eda14ebf6a43d9ada6a2be3d1b06b8b6036eb774/libcxx/include/__algorithm/stable_partition.h#L316 libc++] 与 [https://github.com/gcc-mirror/gcc/blob/d2a499a9881c7c079d2a722b57c7fcf022a864dd/libstdc%2B%2B-v3/include/bits/stl_algo.h#L1608 libstdc++] 中的实现亦作为扩展接受{{named req|ForwardIterator}}所代表的范围。 | [https://github.com/llvm/llvm-project/blob/eda14ebf6a43d9ada6a2be3d1b06b8b6036eb774/libcxx/include/__algorithm/stable_partition.h#L316 libc++] 与 [https://github.com/gcc-mirror/gcc/blob/d2a499a9881c7c079d2a722b57c7fcf022a864dd/libstdc%2B%2B-v3/include/bits/stl_algo.h#L1608 libstdc++] 中的实现亦作为扩展接受{{named req|ForwardIterator}}所代表的范围。 | ||
{{feature test macro|__cpp_lib_constexpr_algorithms|{{ | {{feature test macro|__cpp_lib_constexpr_algorithms|{{|constexpr}} 稳定排序|value=202306L|C++26}} | ||
===示例=== | ===示例=== | ||
{{example | {{example | ||
| | |||
|code= | |||
#include <algorithm> | #include <algorithm> | ||
#include <iostream> | #include <iostream> | ||
| 第57行: | 第73行: | ||
int main() | int main() | ||
{ | { | ||
std::vector<int> v {0, 0, 3, -1, 2, 4, 5, 0, 7}; | std::vector<int> v{0, 0, 3, -1, 2, 4, 5, 0, 7}; | ||
std::stable_partition(v.begin(), v.end(), [](int n) { return n > 0; }); | std::stable_partition(v.begin(), v.end(), [](int n) { return n > 0; }); | ||
for (int n : v) | for (int n : v) | ||
| 第66行: | 第82行: | ||
3 2 4 5 7 0 0 -1 0 | 3 2 4 5 7 0 0 -1 0 | ||
}} | }} | ||
===参阅=== | ===参阅=== | ||