Namespaces
Variants

std:: partition

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
template < class ForwardIt, class UnaryPred >
ForwardIt partition ( ForwardIt first, ForwardIt last, UnaryPred p ) ;
(1) (自 C++20 起为 constexpr)
template < class ExecutionPolicy, class ForwardIt, class UnaryPred >

ForwardIt partition ( ExecutionPolicy && policy,

ForwardIt first, ForwardIt last, UnaryPred p ) ;
(2) (自 C++17 起)
1) 对范围 [ first , last ) 内的元素进行重新排序,使得所有谓词 p 返回 true 的元素均位于谓词 p 返回 false 的元素之前。不保留元素的相对顺序。
2) (1) 相同,但根据 policy 执行。
此重载仅在满足以下所有条件时参与重载决议:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true

(C++20 前)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true

(C++20 起)

如果 * first 的类型不满足 Swappable (C++11 前) ForwardIt 不满足 ValueSwappable (C++11 起) ,则行为未定义。

目录

参数

first, last - 定义待重排元素范围的迭代器对
policy - 要使用的执行策略
p - 一元谓词,若元素应排序于其他元素之前则返回​ true

表达式 p ( v ) 必须对每个类型为(可能为 const 的) VT 的参数 v 可转换为 bool ,其中 VT ForwardIt 的值类型,且与值类别无关,同时不得修改 v 。因此参数类型 VT & 不被允许 VT 也不允许,除非对于 VT 类型移动操作等价于拷贝操作 (C++11 起) 。 ​

类型要求
-
ForwardIt 必须满足 LegacyForwardIterator 的要求。
-
UnaryPred 必须满足 Predicate 的要求。

返回值

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

复杂度

给定 N std:: distance ( first, last )

1) 恰好 N 次对 p 的应用。
最多进行 N/2 次交换(若 ForwardIt 满足 LegacyBidirectionalIterator 的要求),否则最多进行 N 次交换。
2) O(N) 次对 p 的调用。
O(N·log(N)) 次交换。

异常

带有名为 ExecutionPolicy 模板参数的重载按以下方式报告错误:

  • 如果作为算法一部分调用的函数执行抛出异常,且 ExecutionPolicy 是某个 标准策略 ,则会调用 std::terminate 。对于任何其他 ExecutionPolicy ,其行为由实现定义。
  • 如果算法无法分配内存,将抛出 std::bad_alloc

可能的实现

实现重载 (1) 并保持 C++11 兼容性。

template<class ForwardIt, class UnaryPred>
ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPred p)
{
    first = std::find_if_not(first, last, p);
    if (first == last)
        return first;
    for (auto i = std::next(first); i != last; ++i)
        if (p(*i))
        {
            std::iter_swap(i, first);
            ++first;
        }
    return first;
}

示例

#include <algorithm>
#include <forward_list>
#include <iostream>
#include <iterator>
#include <vector>
template<class ForwardIt>
void quicksort(ForwardIt first, ForwardIt last)
{
    if (first == last)
        return;
    auto pivot = *std::next(first, std::distance(first, last) / 2);
    auto middle1 = std::partition(first, last, [pivot](const auto& em)
    {
        return em < pivot;
    });
    auto 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: ";
    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: ";
    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: ";
    for (int n : fl)
        std::cout << n << ' ';
    quicksort(std::begin(fl), std::end(fl));
    std::cout << "\nSorted using quicksort: ";
    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

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 应用于 发布时行为 正确行为
LWG 498 C++98 std::partition 要求 first
last LegacyBidirectionalIterator
仅需满足
LegacyForwardIterator
LWG 2150 C++98 std::partition 仅要求将满足 p 的一个元素
置于不满足 p 的一个元素之前
修正了
该要求

参见

判断范围是否按给定谓词划分
(函数模板)
将元素分为两组并保持其相对顺序
(函数模板)
将元素范围划分为两组
(算法函数对象)