std::experimental::ranges:: all_of, std::experimental::ranges:: any_of, std::experimental::ranges:: none_of
|
Défini dans l'en-tête
<experimental/ranges/algorithm>
|
||
|
template
<
InputIterator I, Sentinel
<
I
>
S,
class
Proj
=
ranges::
identity
,
IndirectUnaryPredicate
<
projected
<
I, Proj
>>
Pred
>
|
(1) | (ranges TS) |
|
template
<
InputRange R,
class
Proj
=
ranges::
identity
,
IndirectUnaryPredicate
<
projected
<
ranges::
iterator_t
<
R
>
, Proj
>>
Pred
>
|
(2) | (ranges TS) |
|
template
<
InputIterator I, Sentinel
<
I
>
S,
class
Proj
=
ranges::
identity
,
IndirectUnaryPredicate
<
projected
<
I, Proj
>>
Pred
>
|
(3) | (ranges TS) |
|
template
<
InputRange R,
class
Proj
=
ranges::
identity
,
IndirectUnaryPredicate
<
projected
<
ranges::
iterator_t
<
R
>
, Proj
>>
Pred
>
|
(4) | (ranges TS) |
|
template
<
InputIterator I, Sentinel
<
I
>
S,
class
Proj
=
identity,
IndirectUnaryPredicate
<
projected
<
I, Proj
>>
Pred
>
|
(5) | (ranges TS) |
|
template
<
InputRange R,
class
Proj
=
ranges::
identity
,
IndirectUnaryPredicate
<
projected
<
ranges::
iterator_t
<
R
>
, Proj
>>
Pred
>
|
(6) | (ranges TS) |
[
first
,
last
)
.
[
first
,
last
)
.
[
first
,
last
)
.
Nonobstant les déclarations dépeintes ci-dessus, le nombre réel et l'ordre des paramètres de template pour les déclarations d'algorithmes ne sont pas spécifiés. Ainsi, si des arguments de template explicites sont utilisés lors de l'appel d'un algorithme, le programme est probablement non portable.
Table des matières |
Paramètres
| first, last | - | la plage des éléments à examiner |
| r | - | la plage des éléments à examiner |
| pred | - | prédicat à appliquer aux éléments projetés |
| proj | - | projection à appliquer aux éléments |
Valeur de retour
Complexité
Implémentation possible
| Première version |
|---|
template<InputIterator I, Sentinel<I> S, class Proj = ranges::identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> bool all_of(I first, S last, Pred pred, Proj proj = Proj{}) { return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj)) == last; } template<InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred> bool all_of(R&& r, Pred pred, Proj proj = Proj{}) { return ranges::all_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } |
| Deuxième version |
template<InputIterator I, Sentinel<I> S, class Proj = ranges::identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> bool any_of(I first, S last, Pred pred, Proj proj = Proj{}) { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last; } template<InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred> bool any_of(R&& r, Pred pred, Proj proj = Proj{}) { return ranges::any_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } |
| Troisième version |
template<InputIterator I, Sentinel<I> S, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> bool none_of(I first, S last, Pred pred, Proj proj = Proj{}) { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) == last; } template<InputRange R, class Proj = ranges::identity, IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred> bool none_of(R&& r, Pred pred, Proj proj = Proj{}) { return ranges::none_of(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } |
Exemple
#include <experimental/ranges/algorithm> #include <experimental/ranges/iterator> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> namespace ranges = std::experimental::ranges; int main() { std::vector<int> v(10, 2); std::partial_sum(v.cbegin(), v.cend(), v.begin()); std::cout << "Parmi les nombres : "; ranges::copy(v, ranges::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; if (ranges::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; })) std::cout << "Tous les nombres sont pairs\n"; if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2))) std::cout << "Aucun n'est impair\n"; struct DivisibleBy { const int d; DivisibleBy(int n) : d(n) {} bool operator()(int n) const { return n % d == 0; } }; if (ranges::any_of(v, DivisibleBy(7))) std::cout << "Au moins un nombre est divisible par 7\n"; }
Sortie :
Parmi les nombres : 2 4 6 8 10 12 14 16 18 20 Tous les nombres sont pairs Aucun n'est impair Au moins un nombre est divisible par 7
Voir aussi
|
(C++11)
(C++11)
(C++11)
|
vérifie si un prédicat est
true
pour tous, certains ou aucun des éléments dans une plage
(modèle de fonction) |