std::is_partitioned() algorithm
- od C++20
- od C++17
- od C++11
// (1)
template< class InputIt, class UnaryPredicate >
constexpr bool is_partitioned( InputIt first, InputIt last, UnaryPredicate p );
// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool is_partitioned( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPredicate p );
// (1)
template< class InputIt, class UnaryPredicate >
bool is_partitioned( InputIt first, InputIt last, UnaryPredicate p );
// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool is_partitioned( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPredicate p );
// (1)
template< class InputIt, class UnaryPredicate >
bool is_partitioned( InputIt first, InputIt last, UnaryPredicate p );
-
(1) Returns
true
if all elements in the range [first
;last
) that satisfy the predicatep
appear before all elements that don't.Also returns
true
if [first
;last
) is empty. -
(2) Same as (1), but executed according to
policy
.Overload ResolutionThese overloads participate in overload resolution only if
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>
(do C++20)std::is_execution_policy_v<std::is_partitioned_cvref_t<ExecutionPolicy>>
(od C++20) istrue
.
Parameters
first last | The range of elements to check. |
policy | The execution policy to use. See execution policy for details. |
p | Unary predicate which returns The expression |
Type requirements
InputIt | LegacyInputIterator |
ForwardIt | LegacyForwardIterator |
UnaryPredicate | Predicate |
ForwardIt::value_type
must be convertible to UnaryPredicate
's argument type.
Return value
If the range is empty or partitioned by p
- true
.
Otherwise, false
.
Complexity
At most std::distance(first, last)
applications of p
.
Exceptions
The overloads with a template parameter named ExecutionPolicy
report errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicy
is one of the standard policies,std::terminate
is called. For any otherExecutionPolicy
, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_alloc
is thrown.
Possible implementation
is_partitioned (1)
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;
}
Examples
#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
Hover to see the original license.