名稱空間
變體
操作

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)
併發擴充套件 2 (併發 TS v2)
概念 (概念 TS)
範圍 (範圍 TS)
反射 (反射 TS)
數學特殊函式 (特殊函式 TR)
實驗性非 TS
模式匹配
線性代數
std::execution
契約
2D 圖形
 
 
 
定義於標頭檔案 <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) 檢查一元謂詞 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
(函式模板) [編輯]