std::ranges::all_of, std::ranges::any_of, std::ranges::none_of
來自 cppreference.com
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(1) | (C++20 起) |
template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< |
(2) | (C++20 起) |
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(3) | (C++20 起) |
template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< |
(4) | (C++20 起) |
template< std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(5) | (C++20 起) |
template< ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< |
(6) | (C++20 起) |
1) 檢查一元謂詞 pred 是否對範圍
[
first,
last)
中的至少一個元素(在透過 proj 投影后)返回 false。3) 檢查一元謂詞 pred 是否對範圍
[
first,
last)
中的至少一個元素(在透過 proj 投影后)返回 true。5) 檢查一元謂詞 pred 是否對範圍
[
first,
last)
中的所有元素(在透過 proj 投影后)返回 false。本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first, last | - | 定義要檢查的元素 範圍 的迭代器-哨兵對 |
r | - | 要檢查的元素範圍 |
pred | - | 應用於投影元素的謂詞 |
proj | - | 應用於元素的投影 |
[編輯] 返回值
1,2) 如果對於範圍中的每個迭代器 i,std::invoke(pred, std::invoke(proj, *i)) != false,則返回 true,否則返回 false。如果範圍為空,則返回 true。
3,4) 如果對於範圍中的至少一個迭代器 i,std::invoke(pred, std::invoke(proj, *i)) != false,則返回 true,否則返回 false。如果範圍為空,則返回 false。
5,6) 如果對於範圍中的每個迭代器 i,std::invoke(pred, std::invoke(proj, *i)) == false,則返回 true,否則返回 false。如果範圍為空,則返回 true。
包含 true 元素 | 是 | 否 | ||
---|---|---|---|---|
包含 false 元素 | 是 | 否 | 是 | 否[1] |
all_of
|
false | true | false | true |
any_of
|
true | true | false | false |
none_of
|
false | false | true | true |
- ↑ 在這種情況下範圍是空的。
[編輯] 複雜度
謂詞和投影至多應用 last - first 次。
[編輯] 可能的實現
all_of (1,2) |
---|
struct all_of_fn { template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const { return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj)) == last; } template<ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>,Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } }; inline constexpr all_of_fn all_of; |
any_of (3,4) |
struct any_of_fn { template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last; } template<ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>,Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } }; inline constexpr any_of_fn any_of; |
none_of (5,6) |
struct none_of_fn { template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const { return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) == last; } template<ranges::input_range R, class Proj = std::identity, std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>,Proj>> Pred> constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const { return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj)); } }; inline constexpr none_of_fn none_of; |
[編輯] 示例
執行此程式碼
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> namespace ranges = std::ranges; constexpr bool some_of(auto&& r, auto&& pred) // some but not all { return not (ranges::all_of(r, pred) or ranges::none_of(r, pred)); } constexpr auto w = {1, 2, 3}; static_assert(!some_of(w, [](int x) { return x < 1; })); static_assert( some_of(w, [](int x) { return x < 2; })); static_assert(!some_of(w, [](int x) { return x < 4; })); 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, std::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"; auto DivisibleBy = [](int d) { return [d](int m) { return m % 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 (函式模板) |