Namespaces
Variants

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

From cppreference.net
Definido en el encabezado <experimental/ranges/algorithm>
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 { } ) ;
(1) (ranges TS)
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 { } ) ;
(2) (ranges TS)
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 { } ) ;
(3) (ranges TS)
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 { } ) ;
(4) (ranges TS)
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 { } ) ;
(5) (ranges TS)
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 { } ) ;
(6) (ranges TS)
1) Comprueba si el predicado unario pred devuelve true para todos los elementos en el rango [ first , last ) .
3) Comprueba si el predicado unario pred devuelve true para al menos un elemento en el rango [ first , last ) .
5) Comprueba si el predicado unario pred devuelve true para ningún elemento en el rango [ first , last ) .
2,4,6) Igual que (1,3,5) , pero utiliza r como rango fuente, como si usara ranges:: begin ( r ) como first y ranges:: end ( r ) como last .

No obstante las declaraciones representadas anteriormente, el número real y el orden de los parámetros de plantilla para las declaraciones de algoritmos no está especificado. Por lo tanto, si se utilizan argumentos de plantilla explícitos al llamar a un algoritmo, el programa probablemente no sea portable.

Contenidos

Parámetros

first, last - el rango de los elementos a examinar
r - el rango de los elementos a examinar
pred - predicado a aplicar a los elementos proyectados
proj - proyección a aplicar a los elementos

Valor de retorno

1,2) true si pred devuelve true para todos los elementos en el rango, false en caso contrario. Devuelve true si el rango está vacío.
3,4) true si pred devuelve true para al menos un elemento en el rango, false en caso contrario. Devuelve false si el rango está vacío.
5,6) true si pred devuelve true para ningún elemento en el rango, false en caso contrario. Devuelve true si el rango está vacío.

Complejidad

1-6) Como máximo last - first aplicaciones del predicado y last - first aplicaciones de la proyección.

Implementación posible

Primera versión
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));
}
Segunda versión
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));
}
Tercera versión
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));
}

Ejemplo

#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 << "Entre los números: ";
    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 << "Todos los números son pares\n";
    if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2)))
        std::cout << "Ninguno de ellos es impar\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 << "Al menos un número es divisible por 7\n";
}

Salida:

Entre los números: 2 4 6 8 10 12 14 16 18 20 
Todos los números son pares
Ninguno de ellos es impar
Al menos un número es divisible por 7

Véase también

(C++11) (C++11) (C++11)
verifica si un predicado es true para todos, alguno o ninguno de los elementos en un rango
(plantilla de función)