Przejdź do głównej zawartości

std::is_partitioned() algorithm

// (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) Returns true if all elements in the range [first; last) that satisfy the predicate p 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 Resolution

    These 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) is true.

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 true if the element value should be replaced.

The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value category, and must not modify v. Thus, a parameter type of VT& is not allowed , nor is VT unless for VT a move is equivalent to a copy. (od C++11).

Type requirements

InputItLegacyInputIterator
ForwardItLegacyForwardIterator
UnaryPredicatePredicate

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 other ExecutionPolicy, 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

Main.cpp
#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';
}
Output
false true false true
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::is_partitioned() algorithm

// (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) Returns true if all elements in the range [first; last) that satisfy the predicate p 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 Resolution

    These 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) is true.

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 true if the element value should be replaced.

The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value category, and must not modify v. Thus, a parameter type of VT& is not allowed , nor is VT unless for VT a move is equivalent to a copy. (od C++11).

Type requirements

InputItLegacyInputIterator
ForwardItLegacyForwardIterator
UnaryPredicatePredicate

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 other ExecutionPolicy, 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

Main.cpp
#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';
}
Output
false true false true
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.