std::is_partitioned
来自cppreference.com
算法库
受约束算法及范围上的算法 (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
受约束算法: std::ranges::copy, std::ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
执行策略 (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
不修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Partitioning operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
划分操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排序操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
二分搜索操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
集合操作(在已排序范围上) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
堆操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
最小/最大操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排列 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
数值运算 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
未初始化存储上的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C 库 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
在标头
<algorithm>
定义
|
||
(1) | ||
template< class InputIt, class UnaryPredicate >
bool is_partitioned( InputIt first, InputIt last, UnaryPredicate p ); |
(C++11 起) (C++20 前) |
|
template< class InputIt, class UnaryPredicate >
constexpr bool is_partitioned( InputIt first, InputIt last, UnaryPredicate p ); |
(C++20 起) | |
template< class ExecutionPolicy,
class ForwardIt, class
UnaryPredicate >
bool is_partitioned( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p ); |
(2) | (C++17 起) |
1) 若范围
[first, last)
中的所有满足 p
的元素都出现在所有不满足的元素前则返回 true 。若 [first, last)
为空亦返回 true 。2) 同 (1) ,但按照
是 true 时才会参与重载决议。
policy
执行。此重载只有在
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> |
(C++20 起) |
参数
first, last | - | 要检查的元素范围 |
policy | - | 所用的执行策略。细节见执行策略。 |
p | - | 若期待在范围开始找到该元素则返回 true 的一元谓词。 对每个(可为 const 的) |
类型要求 | ||
-InputIt 必须符合老式输入迭代器 (LegacyInputIterator)
的要求。
|
||
-ForwardIt 必须符合老式向前迭代器 (LegacyForwardIterator)
的要求。而其值类型必须可转换为 UnaryPredicate 的参数类型
|
||
-UnaryPredicate 必须符合谓词 (Predicate)
的要求。
|
返回值
若范围 [first, last)
为空或以 p
划分则为 true 。否则为 false 。
复杂度
至多应用 std::distance(first, last)
次 p
。
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy
,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
template< class InputIt, class UnaryPredicate > bool is_partitioned(InputIt first, InputIt last, UnaryPredicate 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.begin(), v.end(), is_even); }
输出:
false true false
参阅
将范围中的元素分为两组 (函数模板) |
|
(C++11)
|
定位已划分范围的划分点 (函数模板) |