Namespaces
Variants

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

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace 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 ,
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)
1) Vérifie si le prédicat unaire pred renvoie false pour au moins un élément dans la plage [ first , last ) (après projection avec la projection proj ).
3) Vérifie si le prédicat unaire pred retourne true pour au moins un élément dans la plage [ first , last ) (après projection avec la projection proj ).
5) Vérifie si le prédicat unaire pred renvoie true pour aucun des éléments dans la plage [ first , last ) (après projection avec la projection proj ).
2,4,6) Identique à (1,3,5) , mais utilise r comme plage source, comme si on utilisait ranges:: begin ( r ) comme first et ranges:: end ( r ) comme last .

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

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

1,2) true si std:: invoke ( pred, std:: invoke ( proj, * i ) ) ! = false pour chaque itérateur i dans la plage, false sinon. Retourne true si la plage est vide.
3,4) true si std:: invoke ( pred, std:: invoke ( proj, * i ) ) ! = false pour au moins un itérateur i dans la plage, false sinon. Retourne false si la plage est vide.
5,6) true si std:: invoke ( pred, std:: invoke ( proj, * i ) ) == false pour chaque itérateur i dans la plage, false sinon. Retourne true si la plage est vide.
La plage contient des éléments true Oui Non
La plage contient des éléments 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. La plage est vide dans ce cas.

Complexité

Au plus last - first applications du prédicat et de la projection.

Implémentation possible

**Note:** Le code C++ n'a pas été traduit conformément aux instructions, car il se trouve dans des balises `
` et contient des termes spécifiques au C++ qui doivent être préservés. Seul le texte en dehors des balises de code aurait été traduit, mais dans cet extrait, il n'y a pas de texte à traduire en dehors du code C++.
**Note:** Le code C++ n'a pas été traduit car il se trouve dans des balises `
` et contient des termes spécifiques au C++ qui doivent être préservés selon les instructions. Le code reste donc identique à l'original.
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));
    }
};
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));
    }
};
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));
    }
};
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) // certains mais pas tous
{
    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 << "Parmi les nombres : ";
    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 << "Tous les nombres sont pairs\n";
    if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2)))
        std::cout << "Aucun d'entre eux n'est impair\n";
    auto DivisibleBy = [](int d)
    {
        return [d](int m) { return m % 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 d'entre eux 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 d'une plage
(modèle de fonction)