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

出自cppreference.com
 
 
實驗性
技術規範
文件系統庫 (文件系統 TS)
庫基礎 (庫基礎 TS)
庫基礎 2 (庫基礎 TS v2)
庫基礎 3 (庫基礎 TS v3)
並行擴展 (並行 TS)
並行擴展 2 (並行 TS v2)
並發擴展 (並發 TS)
並發擴展 2 (並發 TS v2)
概念 (概念 TS)
範圍 (範圍 TS)
反射 (反射 TS)
數學特殊函數 (特殊函數 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) (範圍 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) (範圍 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) (範圍 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) (範圍 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) (範圍 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) (範圍 TS)
1) 檢查一元謂詞 pred 是否對範圍 [firstlast) 中的所有元素返回 true
3) 檢查一元謂詞 pred 是否對範圍 [firstlast) 中至少一個元素返回 true
5) 檢查一元謂詞 pred 是否不對範圍 [firstlast) 中任何元素返回 true
2,4,6)(1,3,5),但以 r 為源範圍,如同以 ranges::begin(rng)first 並以 ranges::end(rng)last

參數

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<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 << "这些数中: ";
    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 << "所有数均为偶数\n";
    if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2)))
        std::cout << "其中没有奇数\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 << "至少一个数可被 7 整除\n";
}

輸出:

这些数中: 2 4 6 8 10 12 14 16 18 20 
所有数均为偶数
其中没有奇数
至少一个数可被 7 整除

參閱

(C++11)(C++11)(C++11)
檢查謂詞是否對範圍中所有、任一或無元素為 true
(函數模板) [編輯]