Namespaces
Variants

std:: is_partitioned

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
is_partitioned
(C++11)

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 InputIt, class UnaryPred >
bool is_partitioned ( InputIt first, InputIt last, UnaryPred p ) ;
(1) (C++11 起)
(C++20 起为 constexpr)
template < class ExecutionPolicy, class ForwardIt, class UnaryPred >

bool is_partitioned ( ExecutionPolicy && policy,

ForwardIt first, ForwardIt last, UnaryPred p ) ;
(2) (C++17 起)
1) 检查 [ first , last ) 是否按谓词 p 进行了分区:所有满足 p 的元素都出现在不满足的元素之前。
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, last - 定义待检验元素范围的迭代器对
policy - 要使用的执行策略
p - 一元谓词,对于期望在范围起始处找到的元素应返回 ​ true

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

类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。
-
ForwardIt 必须满足 LegacyForwardIterator 的要求,且其值类型必须可转换为 UnaryPred 的参数类型。
-
UnaryPred 必须满足 Predicate 的要求。

返回值

若范围 [ first , last ) 中的元素 e 相对于表达式 p ( e ) 满足 划分条件 ,则返回 true ,否则返回 false

复杂度

最多应用 std:: distance ( first, last ) p

异常

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

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

可能的实现

template<class InputIt, class UnaryPred>
bool is_partitioned(InputIt first, InputIt last, UnaryPred p)
{
    for (; first != last; ++first)
        if (!p(*first))
            break;
    for (; first != last; ++first)
        if (p(*first))
            return false;
    return true;
}

示例

#include <algorithm>
#include <array>
#include <iostream>
int main()
{
    std::array<int, 9> v {1, 2, 3, 4, 5, 6, 7, 8, 9};
    auto is_even = [](int i) { return i % 2 == 0; };
    std::cout.setf(std::ios_base::boolalpha);
    std::cout << std::is_partitioned(v.begin(), v.end(), is_even) << ' ';
    std::partition(v.begin(), v.end(), is_even);
    std::cout << std::is_partitioned(v.begin(), v.end(), is_even) << ' ';
    std::reverse(v.begin(), v.end());
    std::cout << std::is_partitioned(v.cbegin(), v.cend(), is_even) << ' ';
    std::cout << std::is_partitioned(v.crbegin(), v.crend(), is_even) << '\n';
}

输出:

false true false true

参见

将元素范围划分为两组
(函数模板)
定位已划分范围的分割点
(函数模板)
判断范围是否按给定谓词划分
(算法函数对象)