首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::stable_partition

Defined in header <algorithm>

?

?

template< class BidirIt, class UnaryPredicate > BidirIt stable_partition( BidirIt first, BidirIt last, UnaryPredicate p );

(1)

?

template< class ExecutionPolicy, class BidirIt, class UnaryPredicate > BidirIt stable_partition( ExecutionPolicy&& policy, BidirIt first, BidirIt last, UnaryPredicate p );

(2)

(since C++17)

1%29重新排序范围内的元素[first, last)以这样的方式使谓词所针对的所有元素p回报true在谓词的元素之前p回报false.元素的相对顺序被保留。

2%29与%281%29相同,但根据policy此重载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last

-

the range of elements to reorder

policy

-

the execution policy to use. See execution policy for details.

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. The type Type must be such that an object of type BidirIt can be dereferenced and then implicitly converted to Type. ?

类型要求

-Bidirit必须符合价值可互换和双向投资者的要求。

-取消引用的Bidirit必须符合可移动和可移动的要求。

-单数预测必须满足谓词的要求。

返回值

第二组的第一个元素的迭代器。

复杂性

给出N = last - first...

1%29N谓词和O(N)如果有足够的额外内存,就交换。如果内存不足,最多只能N log N交换。

2%29O(N log N)互换和O(N)谓词的应用

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

注记

此函数尝试分配临时缓冲区。如果分配失败,则选择效率较低的算法。

二次

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <vector>
 
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';
}

二次

产出:

二次

代码语言:javascript
复制
3 2 4 5 7 0 0 0 0

二次

另见

partition

divides a range of elements into two groups (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com