std::ranges:: is_partitioned
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Défini dans l'en-tête
<algorithm>
|
||
|
Signature d'appel
|
||
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
class
Proj
=
std::
identity
,
|
(1) | (depuis C++20) |
|
template
<
ranges::
input_range
R,
class
Proj
=
std::
identity
,
std::
indirect_unary_predicate
<
|
(2) | (depuis C++20) |
[
first
,
last
)
qui satisfont le prédicat
pred
après projection apparaissent avant tous les éléments qui ne le satisfont pas. Retourne également
true
si
[
first
,
last
)
est vide.
Les entités de type fonction décrites sur cette page sont des algorithm function objects (informellement appelées niebloids ), c'est-à-dire :
- Les listes d'arguments de modèle explicites ne peuvent pas être spécifiées lors de l'appel de l'une d'entre elles.
- Aucune d'entre elles n'est visible pour la recherche dépendante des arguments .
- Lorsque l'une d'entre elles est trouvée par la recherche non qualifiée normale comme nom à gauche de l'opérateur d'appel de fonction, la recherche dépendante des arguments est inhibée.
Table des matières |
Paramètres
| first, last | - | la paire itérateur-sentinelle définissant l'intervalle des éléments à examiner |
| r | - | l'intervalle des éléments à examiner |
| pred | - | prédicat à appliquer aux éléments projetés |
| proj | - | projection à appliquer aux éléments |
Valeur de retour
true
si la plage
[
first
,
last
)
est vide ou est partitionnée par
pred
,
false
sinon.
Complexité
Au plus ranges:: distance ( first, last ) applications de pred et proj .
Implémentation possible
struct is_partitioned_fn { template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const { for (; first != last; ++first) if (!std::invoke(pred, std::invoke(proj, *first))) break; for (; first != last; ++first) if (std::invoke(pred, std::invoke(proj, *first))) return false; return true; } template<ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } }; inline constexpr auto is_partitioned = is_partitioned_fn(); |
Exemple
#include <algorithm> #include <array> #include <iostream> #include <numeric> #include <utility> int main() { std::array<int, 9> v; auto print = [&v](bool o) { for (int x : v) std::cout << x << ' '; std::cout << (o ? "=> " : "=> not ") << "partitioned\n"; }; auto is_even = [](int i) { return i % 2 == 0; }; std::iota(v.begin(), v.end(), 1); // or std::ranges::iota(v, 1); print(std::ranges::is_partitioned(v, is_even)); std::ranges::partition(v, is_even); print(std::ranges::is_partitioned(std::as_const(v), is_even)); std::ranges::reverse(v); print(std::ranges::is_partitioned(v.cbegin(), v.cend(), is_even)); print(std::ranges::is_partitioned(v.crbegin(), v.crend(), is_even)); }
Sortie :
1 2 3 4 5 6 7 8 9 => not partitioned 2 4 6 8 5 3 7 1 9 => partitioned 9 1 7 3 5 8 6 4 2 => not partitioned 9 1 7 3 5 8 6 4 2 => partitioned
Voir aussi
|
(C++20)
|
divise une plage d'éléments en deux groupes
(objet fonction algorithme) |
|
(C++20)
|
localise le point de partition d'une plage partitionnée
(objet fonction algorithme) |
|
(C++11)
|
détermine si la plage est partitionnée par le prédicat donné
(modèle de fonction) |