命名空间
变体
操作

std::experimental::ranges::all_of、std::experimental::ranges::any_of、std::experimental::ranges::none_of

来自 cppreference.cn
< cpp‎ | experimental‎ | ranges
 
 
实验性
技术规范
文件系统库 (filesystem TS)
库基础 (library fundamentals TS)
库基础 2 (library fundamentals TS v2)
库基础 3 (library fundamentals TS v3)
并行性扩展 (parallelism TS)
并行性扩展 2 (parallelism TS v2)
并发性扩展 (concurrency TS)
并发性扩展 2 (concurrency TS v2)
概念 (concepts TS)
范围 (ranges TS)
反射 (reflection TS)
数学特殊函数 (special functions TR)
实验性非 TS
模式匹配
线性代数
std::execution
契约
2D 图形
 
 
 
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) 检查一元谓词 pred 对于范围 [firstlast) 中的所有元素是否返回 true
3) 检查一元谓词 pred 对于范围 [firstlast) 中至少一个元素是否返回 true
5) 检查一元谓词 pred 对于范围 [firstlast) 中没有元素返回 true
2,4,6)(1,3,5) 相同,但使用 r 作为源范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

尽管上面描述了声明,但算法声明的模板参数的实际数量和顺序是未指定的。 因此,如果在调用算法时使用显式模板参数,则程序可能是不可移植的。

目录

[编辑] 参数

first, last - 要检查的元素范围
r - 要检查的元素范围
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<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));
}
第二版本
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));
}
第三版本
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));
}

[编辑] 示例

#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 << "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
(函数模板) [编辑]