cpp/algorithm/stable partition:修订间差异

来自cppreference.com
Lynnboy留言 | 贡献
无编辑摘要
Xmcgcg留言 | 贡献
无编辑摘要
第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 UnaryPredicate >
template< class BidirIt, class >
BidirIt stable_partition( BidirIt first, BidirIt last, UnaryPredicate p );
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 UnaryPredicate >
template< class ExecutionPolicy, class BidirIt, class >
BidirIt stable_partition( ExecutionPolicy&& policy,
BidirIt stable_partition( ExecutionPolicy&& policy,
                           BidirIt first, BidirIt last, UnaryPredicate p );
                           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 |元素应先序于其他元素| p1=BidirIt}}
{{par pred1|p|元素应其他元素|p1=BidirIt}}
{{par hreq}}
{{par hreq}}
{{par req named | BidirIt | BidirectionalIterator| ValueSwappable}}
{{par req named|BidirIt|BidirectionalIterator}}
{{par req named deref | BidirIt | MoveConstructible| MoveAssignable}}
{{par req named||Predicate}}
{{par req named | UnaryPredicate | Predicate}}
{{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@ 若有充足内存,则恰好应用 {{mathjax-or|\(\scriptsize N\)|N}} 次谓词并进行 {{mathjax-or|\(\scriptsize O(N)\)|O(N)}} 次交换。若内存不充足,则至进行 {{mathjax-or|\(\scriptsize N log(N)\)|N log(N)}} 次交换
 
@2@ {{mathjax-or|\(\scriptsize N log(N)\)|N log(N)}} 次交换及应用 {{mathjax-or|\(\scriptsize O(N)\)|O(N)}} 次谓词
@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|{{tt|constexpr}} 稳定排序|value=202306L|C++26}}
{{feature test macro|__cpp_lib_constexpr_algorithms|{{|constexpr}} 稳定排序|value=202306L|C++26}}


===示例===
===示例===
{{example
{{example
|
|
| code=
|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
}}
}}


===参阅===
===参阅===

2024年3月29日 (五) 08:48的版本

 
 
算法库
受约束算法及范围上的算法 (C++20)
包含算法例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)