std::is_partitioned(3)
π¦ std::is_partitioned(3) β C++ Standard Libary π¦
okay so bestie, welcome to the C++ Standard Libary page for std::is_partitioned β the algorithm that does a whole vibe check on whether your range is partitioned. no cap, itβs giving very specific energy. π
πΈ NAME πΈ
std::is_partitioned β std::is_partitioned
Thatβs it. Thatβs the name. iconic. β¨
β¨ Synopsis β¨
Defined in header <algorithm>.
template< class InputIt, class UnaryPred >
bool is_partitioned( InputIt first, InputIt last,
UnaryPred p );
(1) β (since C++11) β (constexpr since C++20)
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool is_partitioned( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last,
UnaryPred p );
(2) β (since C++17)
- Checks whether
[first, last)is partitioned by the predicatep: all elements that satisfypappear before all elements that do not. - Same as (1), but executed according to
policy.
Overload (2) participates in overload resolution only if:
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>istrue. (until C++20)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>istrue. (since C++20)
π Parameters π
first,lastβ the range of elements to check.policyβ the execution policy to use. See execution policy for details.pβ unary predicate which returnstruefor the elements expected to be found in the beginning of the range.
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 (since C++11).
π¦ Type requirements π¦
InputItmust meet the requirements ofLegacyInputIterator.ForwardItmust meet the requirements ofLegacyForwardIterator. and its value type must be convertible toUnaryPred's parameter type.UnaryPredmust meet the requirements ofPredicate.
π Return value π
true if the elements e of [first, last) are partitioned with respect to the expression p(e). false otherwise.
π₯ Complexity π₯
At most std::distance(first, last) applications of p.
π«Ά Exceptions π«Ά
The overload with a template parameter named ExecutionPolicy reports errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicyis one of the standard policies,std::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_allocis thrown.
π Possible implementation π
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;
}
π Example π
// 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.cbegin(), v.cend(), is_even) << ' ';
std::cout << std::is_partitioned(v.crbegin(), v.crend(), is_even) << '\n';
}
π§ Output π§
false true false true
π See also π
partitionβ divides a range of elements into two groups (function template)partition_pointβ locates the partition point of a partitioned range (C++11) (function template)ranges::is_partitionedβ determines if the range is partitioned by the given predicate (C++20) (niebloid)
thatβs the whole page, bestie π«Ά β source: http://cppreference.com, version/date: 2024.06.10, and the manual title is std::is_partitioned(3). periodt. π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms