std::stable_partition
来自cppreference.com
![]() | 该页由英文版wiki使用Google Translate机器翻译而来。 该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <algorithm> 中定义 | ||
template<class BidirIt, class UnaryPredicate > BidirIt stable_partition( BidirIt first, BidirIt last, UnaryPredicate p ); | ||
重新排序的范围
[first, last)
在这样一种方式中的元素,所有元素的谓词p
回报true之前的元素为谓词p
回报false。元素的相对顺序被保存. 原文:
Reorders the elements in the range
[first, last)
in such a way that all elements for which the predicate p
returns true precede the elements for which predicate p
returns false. Relative order of the elements is preserved. 目录 |
[编辑]参数
first, last | - | 范围内的元素进行重新排序 |
p | - | unary predicate which returns true 如果该元素的,要责令之前的其他元素 . 原文: if the element should be ordered before other elements The signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const&, but the function must not modify the objects passed to it. |
类型要求 | ||
-BidirIt 必须满足 ValueSwappable 和 BidirectionalIterator 的要求。 | ||
-The type of dereferenced BidirIt must meet the requirements of MoveAssignable and MoveConstructible . |
[编辑]返回值
迭代器到第二组的第一个元素
原文:
Iterator to the first element of the second group
[编辑]复杂度
究竟
last-first
应用程序的谓词和最(last-first)*log(last-first)
掉期,如果有足够的内存或线性的掉期,如果有足够的可用内存.原文:
Exactly
last-first
applications of the predicate and at most (last-first)*log(last-first)
swaps if there is insufficient memory or linear number of swaps if sufficient memory is available.[编辑]示例
#include <iostream>#include <algorithm> int main(){std::vector<int> v{0, 0, 3, 0, 2, 4, 5, 0, 7}; std::stable_partition(v.begin(), v.end(), [](int n){return n>0;});for(int n : v){std::cout<< n <<' ';}std::cout<<'\n';}
输出:
3 2 4 5 7 0 0 0 0
[编辑]另请参阅
把一个区间的元素分为两组 (函数模板) |