CppDS.com

C++ 98 11 14 17 20 手册

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

来自cppreference.com
< cpp‎ | experimental‎ | ranges
 
 
技术规范
文件系统库 (文件系统 TS)
库基础 (库基础 TS)
库基础 2 (库基础 TS v2)
库基础 3 (库基础 TS v3)
并行扩展 (并行 TS)
并行扩展 2 (并行 TS v2)
并发扩展 (并发 TS)
概念 (概念 TS)
范围 (范围 TS)
数学特殊函数 (特殊函数 TR)
 
 
 
template< ranges::InputIterator I, ranges::Sentinel<I> S,

    class Proj = ranges::identity,
    ranges::IndirectUnaryPredicate<ranges::projected<I, Proj>> Pred >

bool all_of( I first, S last, Pred pred, Proj proj = Proj{} );
(1) (范围 TS)
template< ranges::InputRange Rng, class Proj = ranges::identity,

    ranges::IndirectUnaryPredicate<
        ranges::projected<ranges::iterator_t<Rng>, Proj>
    > Pred >

bool all_of( Rng&& rng, Pred pred, Proj proj = Proj{} );
(2) (范围 TS)
template< ranges::InputIterator I, ranges::Sentinel<I> S,

    class Proj = ranges::identity,
    ranges::IndirectUnaryPredicate<ranges::projected<I, Proj>> Pred >

bool any_of( I first, S last, Pred pred, Proj proj = Proj{} );
(3) (范围 TS)
template< ranges::InputRange Rng, class Proj = ranges::identity,

    ranges::IndirectUnaryPredicate<
        ranges::projected<ranges::iterator_t<Rng>, Proj>
    > Pred >

bool any_of( Rng&& rng, Pred pred, Proj proj = Proj{} );
(4) (范围 TS)
template< ranges::InputIterator I, ranges::Sentinel<I> S,

    class Proj = identity,
    ranges::IndirectUnaryPredicate<ranges::projected<I, Proj>> Pred >

bool none_of( I first, S last, Pred pred, Proj proj = Proj{} );
(5) (范围 TS)
template< ranges::InputRange Rng, class Proj = ranges::identity,

    ranges::IndirectUnaryPredicate<
        ranges::projected<ranges::iterator_t<Rng>, Proj>
    > Pred >

bool none_of( Rng&& rng, Pred pred, Proj proj = Proj{} );
(6) (范围 TS)
1) 检查一元谓词 pred 是否对范围 [first, last) 中的所有元素返回 true
3) 检查一元谓词 pred 是否对范围 [first, last) 中至少一个元素返回 true
5) 检查一元谓词 pred 是否不对范围 [first, last) 中任何元素返回 true
2,4,6)(1,3,5) ,但以 rng 为源范围,如同使用 ranges::begin(rng)ranges::end(rng)

参数

first, last - 要检验的元素范围
rng - 要检验的元素范围
pred - 应用到投影后元素的谓词
proj - 应用到元素的投影

返回值

1-2)pred 对范围中所有元素返回 true 则为 true ,否则为 false 。若范围为空则返回 true
3-4)pred 对范围中至少一个元素返回 true 则为 true ,否则为 false 。若范围为空则返回 false
5-6)pred 不对范围中任何元素返回 true 则为 true ,否则为 false 。若范围为空则返回 true

复杂度

1-6) 至多应用 last - first 次谓词,应用 last - first 次投影。

可能的实现

版本一
template< ranges::InputIterator I, ranges::Sentinel<I> S, class Proj = ranges::identity,
   ranges::IndirectUnaryPredicate<ranges::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< ranges::InputRange Rng, class Proj = ranges::identity,
   ranges::IndirectUnaryPredicate<ranges::projected<ranges::iterator_t<Rng>, Proj>> Pred >
bool all_of( Rng&& rng, Pred pred, Proj proj = Proj{} )
{
   return ranges::all_of(ranges::begin(rng), ranges::end(rng), std::ref(pred), std::ref(proj));
}
版本二
template< ranges::InputIterator I, ranges::Sentinel<I> S, class Proj = ranges::identity,
   ranges::IndirectUnaryPredicate<ranges::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< ranges::InputRange Rng, class Proj = ranges::identity,
   ranges::IndirectUnaryPredicate<ranges::projected<ranges::iterator_t<Rng>, Proj>> Pred >
bool any_of( Rng&& rng, Pred pred, Proj proj = Proj{} )
{
   return ranges::any_of(ranges::begin(rng), ranges::end(rng), std::ref(pred), std::ref(proj));
}
版本三
template< ranges::InputIterator I, ranges::Sentinel<I> S, class Proj = identity,
   ranges::IndirectUnaryPredicate<ranges::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< ranges::InputRange Rng, class Proj = ranges::identity,
   ranges::IndirectUnaryPredicate<ranges::projected<ranges::iterator_t<Rng>, Proj>> Pred >
bool none_of( Rng&& rng, Pred pred, Proj proj = Proj{} )
{
   return ranges::none_of(ranges::begin(rng), ranges::end(rng), std::ref(pred), std::ref(proj));
}

示例

#include <vector>
#include <numeric>
#include <experimental/ranges/algorithm>
#include <experimental/ranges/iterator>
#include <iterator>
#include <iostream>
#include <functional>
 
namespace ranges = std::experimental::ranges;
 
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, 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 << "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";
    }
    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 << "At least one number is divisible by 7\n";
    }
}

输出:

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

参阅

(C++11)(C++11)(C++11)
检查谓词是否对范围中所有、任一或无元素为 true
(函数模板)
关闭