std::partition
STD::分区
Defined in header | | |
---|---|---|
| (1) | |
template< class BidirIt, class UnaryPredicate > BidirIt partition( BidirIt first, BidirIt last, UnaryPredicate p | (until C++11) | |
template< class ForwardIt, class UnaryPredicate > ForwardIt partition( ForwardIt first, ForwardIt last, UnaryPredicate p | (since C++11) | |
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > ForwardIt partition( ExecutionPolicy&& policy, ForwardIt first, ForwardIt 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 ForwardIt can be dereferenced and then implicitly converted to Type. |
类型要求
-Bidirit必须符合双向迭代器的要求。
---。但是,如果能满足双向迭代器的要求,则操作效率更高。
-单数预测必须满足谓词的要求。
返回值
第二组的第一个元素的迭代器。
复杂性
给定N=std::distance
(first,last)
,,,
1%,29谓词的N个应用程序。最多N/2互换ForwardIt
满足…的要求BidirectionalIterator
,最多也是N次掉期。
2%29O(N log N)
掉期and O(N)
谓词的应用。
例外
带有名为ExecutionPolicy
报告错误如下:
- 如果执行作为算法一部分调用的函数,则引发异常
ExecutionPolicy
是其中之一标准政策,,,std::terminate
叫做。对于任何其他人ExecutionPolicy
,行为是由实现定义的。
- 如果算法不能分配内存,
std::bad_alloc
被扔了。
可能的实施
模板<类前进,类单数预测>前进,分区%28前进,前进最后,单数预测p%29{First=std::find[医]如果[医]非%281,Late,p%29;if%28first==最后%29,for%28 Forwardit i=std::Next%281%29;i%21=Lest;++i%29{if%28p%28%2AI%29%29{STD::ITER[医]交换%28i,第一个%29;++优先;}先返回;}
*。
例
二次
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <forward_list>
template <class ForwardIt>
void quicksort(ForwardIt first, ForwardIt last)
{
if(first == last) return;
auto pivot = *std::next(first, std::distance(first,last)/2
ForwardIt middle1 = std::partition(first, last,
[pivot](const auto& em){ return em < pivot; }
ForwardIt middle2 = std::partition(middle1, last,
[pivot](const auto& em){ return !(pivot < em }
quicksort(first, middle1
quicksort(middle2, last
}
int main()
{
std::vector<int> v = {0,1,2,3,4,5,6,7,8,9};
std::cout << "Original vector:\n ";
for (int elem : v) std::cout << elem << ' ';
auto it = std::partition(v.begin(), v.end(), [](int i){return i % 2 == 0;}
std::cout << "\nPartitioned vector:\n ";
std::copy(std::begin(v), it, std::ostream_iterator<int>(std::cout, " ")
std::cout << " * ";
std::copy(it, std::end(v), std::ostream_iterator<int>(std::cout, " ")
std::forward_list<int> fl = {1, 30, -4, 3, 5, -4, 1, 6, -8, 2, -5, 64, 1, 92};
std::cout << "\nUnsorted list:\n ";
for(int n : fl) std::cout << n << ' ';
std::cout << '\n';
quicksort(std::begin(fl), std::end(fl)
std::cout << "Sorted using quicksort:\n ";
for(int fi : fl) std::cout << fi << ' ';
std::cout << '\n';
}
二次
产出:
二次
Original vector:
0 1 2 3 4 5 6 7 8 9
Partitioned vector:
0 8 2 6 4 * 5 3 7 1 9
Unsorted list:
1 30 -4 3 5 -4 1 6 -8 2 -5 64 1 92
Sorted using quicksort:
-8 -5 -4 -4 1 1 1 2 3 5 6 30 64 92
二次
另见
is_partitioned (C++11) | determines if the range is partitioned by the given predicate (function template) |
---|---|
stable_partition | divides elements into two groups while preserving their relative order (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。