Namespaces
Variants

std::ranges::all_of, std::ranges::any_of, std::ranges::none_of

De fr.cppreference.net
 
 
Bibliothèque d'algorithmes
Algorithmes contraints et algorithmes sur intervalles (C++20)
Algorithmes contraints, p. ex. ranges::copy, ranges::sort, ...
Opérations séquentielles non modificatrices    
Opérations par lots
(C++17)
Opérations de recherche
Opérations séquentielles modificatrices
Opérations de copie
(C++11)
(C++11)
Opérations d'échange
Opérations de transformation
Opérations de génération
Opérations de suppression
Opérations de changement d'ordre
(jusqu'à C++17)(C++11)
(C++20)(C++20)
Opérations d'échantillonnage
(C++17)

Tri et opérations associées
Opérations de partitionnement
(C++11)    

Opérations de tri
Opérations de recherche binaire
(sur intervalles partitionnés)
Opérations ensemblistes (sur intervalles triés)
Opérations de fusion (sur intervalles triés)
Opérations de tas
Opérations minimum/maximum
(C++11)
(C++17)
Opérations de comparaison lexicographique
Opérations de permutation


 
Algorithmes contraints
Tous les noms de ce menu appartiennent à l'espace de noms std::ranges
Opérations séquentielles non modificatrices
Opérations séquentielles modificatrices
Opérations de partitionnement
Opérations de tri
Opérations de recherche binaire (sur intervalles triés)
       
       
Opérations ensemblistes (sur intervalles triés)
Opérations de tas
Opérations minimum/maximum
       
       
Opérations de permutation
Opérations de pliage
Opérations sur le stockage non initialisé
Types de retour
 
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,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
constexpr bool all_of( I first, S last, Pred pred, Proj proj = {} );
(1) (depuis C++20)
template< ranges::input_range R, class Proj = std::identity,
          std::indirect_unary_predicate
              <std::projected<ranges::iterator_t<R>, Proj>> Pred >
constexpr bool all_of( R&& r, Pred pred, Proj proj = {} );
(2) (depuis C++20)
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 any_of( I first, S last, Pred pred, Proj proj = {} );
(3) (depuis C++20)
template< ranges::input_range R, class Proj = std::identity,
          std::indirect_unary_predicate
              <std::projected<ranges::iterator_t<R>, Proj>> Pred >
constexpr bool any_of( R&& r, Pred pred, Proj proj = {} );
(4) (depuis C++20)
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 none_of( I first, S last, Pred pred, Proj proj = {} );
(5) (depuis C++20)
template< ranges::input_range R, class Proj = std::identity,
          std::indirect_unary_predicate
              <std::projected<ranges::iterator_t<R>, Proj>> Pred >
constexpr bool none_of( R&& r, Pred pred, Proj proj = {} );
(6) (depuis C++20)
template< /*execution-policy*/ Ep, std::random_access_iterator I,
          std::sized_sentinel_for<I> S, class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
bool all_of( Ep&& policy, I first, S last, Pred pred, Proj proj = {} );
(7) (depuis C++26)
template< /*execution-policy*/ Ep,
          /*sized-random-access-range*/ R, class Proj = std::identity,
          std::indirect_unary_predicate
              <std::projected<ranges::iterator_t<R>, Proj>> Pred >
bool all_of( Ep&& policy, R&& r, Pred pred, Proj proj = {} );
(8) (depuis C++26)
template< /*execution-policy*/ Ep, std::random_access_iterator I,
          std::sized_sentinel_for<I> S, class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
bool any_of( Ep&& policy, I first, S last, Pred pred, Proj proj = {} );
(9) (depuis C++26)
template< /*execution-policy*/ Ep,
          /*sized-random-access-range*/ R, class Proj = std::identity,
          std::indirect_unary_predicate
              <std::projected<ranges::iterator_t<R>, Proj>> Pred >
bool any_of( Ep&& policy, R&& r, Pred pred, Proj proj = {} );
(10) (depuis C++26)
template< /*execution-policy*/ Ep, std::random_access_iterator I,
          std::sized_sentinel_for<I> S, class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
bool none_of( Ep&& policy, I first, S last, Pred pred, Proj proj = {} );
(11) (depuis C++26)
template< /*execution-policy*/ Ep,
          /*sized-random-access-range*/ R, class Proj = std::identity,
          std::indirect_unary_predicate
              <std::projected<ranges::iterator_t<R>, Proj>> Pred >
bool none_of( Ep&& policy, R&& r, Pred pred, Proj proj = {} );
(12) (depuis C++26)

Pour la définition de /*execution-policy*/, voir cette page; pour la définition de /*sized-random-access-range*/, voir cette page.

1,2) Vérifie si le prédicat unaire pred retourne false pour tous les éléments (projetés par proj) dans l'intervalle [firstlast) ou r.
3,4) Vérifie si le prédicat unaire pred retourne true pour au moins un élément (projeté par proj) dans l'intervalle [firstlast) ou r.
5,6) Vérifie si le prédicat unaire pred retourne true pour aucun des éléments (projetés par proj) dans l'intervalle [firstlast) ou r.
7-12) Identique à (1-6), mais exécuté selon policy.

Les entités de type fonction décrites sur cette page sont des objets fonction d'algorithme (informellement appelés niebloids), c'est-à-dire :

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 - le prédicat à appliquer aux éléments (projetés)
proj - la projection à appliquer aux éléments
policy - la politique d'exécution à utiliser

Valeur de retour

L'intervalle a-t-il un élément true ? Oui Non
 L'intervalle a-t-il un élément false Oui Non Oui     Non[1]
all_of false true false true
any_of true true   false     false  
none_of   false     false   true true
  1. L'intervalle est vide dans ce cas.

Complexité

Au plus range::distance(first, last) ou range::distance(r) applications de pred et de la proj.

Exceptions

7-12) Pendant le processus d'exécution :
  • Si les ressources mémoire temporaires nécessaires à la parallélisation ne sont pas disponibles, std::bad_alloc est levée.
  • Si une exception non capturée est levée lors de l'accès aux objets via un argument d'algorithme, le comportement est déterminé par la politique d'exécution (pour les politiques standard, std::terminate est invoqué).

Implémentation possible

all_of (1,2)
struct all_of_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
    {
        return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj)) == last;
    }
    
    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 operator()(ranges::begin(r), ranges::end(r),
                          std::ref(pred), std::ref(proj));
    }
    
    template<ranges::forward_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 operator()(ranges::begin(r),
                          ranges::next(ranges::begin(r), ranges::end(r)),
                          std::ref(pred), std::ref(proj));
    }
};

inline constexpr all_of_fn all_of;
any_of (3,4)
struct any_of_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
    {
        return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last;
    }
    
    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 operator()(ranges::begin(r), ranges::end(r),
                          std::ref(pred), std::ref(proj));
    }
    
    template<ranges::forward_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 operator()(ranges::begin(r),
                          ranges::next(ranges::begin(r), ranges::end(r)),
                          std::ref(pred), std::ref(proj));
    }
};

inline constexpr any_of_fn any_of;
none_of (5,6)
struct none_of_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
    {
        return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) == last;
    }
    
    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 operator()(ranges::begin(r), ranges::end(r),
                          std::ref(pred), std::ref(proj));
    }
    
    template<ranges::forward_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 operator()(ranges::begin(r),
                          ranges::next(ranges::begin(r), ranges::end(r)),
                          std::ref(pred), std::ref(proj));
    }
};

inline constexpr none_of_fn none_of;

Exemple

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

namespace ranges = std::ranges;

constexpr bool some_of(auto&& r, auto&& pred) // some but not all
{
    return not (ranges::all_of(r, pred) or ranges::none_of(r, pred));
}

constexpr auto w = {1, 2, 3};
static_assert(!some_of(w, [](int x) { return x < 1; }));
static_assert( some_of(w, [](int x) { return x < 2; }));
static_assert(!some_of(w, [](int x) { return x < 4; }));

int main()
{
    std::vector<int> v(10, 2);
    std::partial_sum(v.cbegin(), v.cend(), v.begin());
    std::cout << "Among the numbers: ";
    ranges::copy(v, std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
    
    if (ranges::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; }))
        std::cout << "All numbers are even\n";
    
    if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2)))
        std::cout << "None of them are odd\n";
    
    auto DivisibleBy = [](int d)
    {
        return [d](int m) { return m % d == 0; };
    };
    
    if (ranges::any_of(v, DivisibleBy(7)))
        std::cout << "At least one number is divisible by 7\n";
}

Sortie :

Among the numbers: 2 4 6 8 10 12 14 16 18 20
All numbers are even
None of them are odd
At least one number is divisible by 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 d'un intervalle
(modèle de fonction)