std::is_partitioned
: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| (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)
policy std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> true | first, last | - | |
| policy | - | |
| p | - | true |
-InputIt LegacyInputIterator
| ||
-ForwardIt LegacyForwardIterator UnaryPredicate's
| ||
-UnaryPredicate Predicate
| ||
[first, last) p true false
std::distance(first, last) p
ExecutionPolicy
-
ExecutionPolicystd::terminateExecutionPolicy - 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;
}
|
Run this code
#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
| 2 () | |
(C++11) |
() |