C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- std::execution::sequenced_policy, std::execution::parallel_policy, std::execution::parallel_unsequenced_policy, std::execution::unsequenced_policy
- æå¶çº¦ç®æ³ (C++20 èµ·)
- std::is_execution_policy
- std::execution::seq, std::execution::par, std::execution::par_unseq, std::execution::unseq
- std::all_of, std::any_of, std::none_of
- std::for_each_n
- std::sort
- std::reduce
- std::inclusive_scan
- std::exclusive_scan
- std::random_shuffle, std::shuffle
- std::clamp
- std::equal
- std::is_permutation
- std::mismatch
- std::swap
- std::search
- std::transform
- std::for_each
- std::make_heap
- std::count, std::count_if
- std::adjacent_find
- std::find, std::find_if, std::find_if_not
- std::find_end
- std::find_first_of
- std::search_n
- std::lexicographical_compare
- std::lexicographical_compare_three_way
- std::copy, std::copy_if
- std::copy_n
- std::copy_backward
- std::move
- std::move_backward
- std::shift_left, std::shift_right
- std::fill
- std::fill_n
- std::generate
- std::generate_n
- std::iter_swap
- std::swap_ranges
- std::sample
- std::remove, std::remove_if
- std::replace, std::replace_if
- std::reverse
- std::rotate
- std::unique
- std::remove_copy, std::remove_copy_if
- std::replace_copy, std::replace_copy_if
- std::reverse_copy
- std::rotate_copy
- std::unique_copy
- std::is_partitioned
- std::partition_point
- std::partition
- std::partition_copy
- std::stable_partition
- std::is_sorted
- std::is_sorted_until
- std::stable_sort
- std::partial_sort
- std::partial_sort_copy
- std::nth_element
- std::lower_bound
- std::upper_bound
- std::binary_search
- std::equal_range
- std::merge
- std::inplace_merge
- std::set_difference
- std::set_intersection
- std::set_symmetric_difference
- std::set_union
- std::includes
- std::is_heap
- std::is_heap_until
- std::sort_heap
- std::push_heap
- std::pop_heap
- std::max
- std::max_element
- std::min
- std::min_element
- std::minmax
- std::minmax_element
- std::next_permutation
- std::prev_permutation
- std::iota
- std::inner_product
- std::adjacent_difference
- std::accumulate
- std::transform_reduce
- std::partial_sum
- std::transform_inclusive_scan
- std::transform_exclusive_scan
- std::qsort
- std::bsearch
- 注é
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >ç®æ³åº > std::all_of, std::any_of, std::none_of
std::all_of, std::any_of, std::none_of
| å®ä¹äºå¤´æä»¶ <algorithm>
|
||
| (1) | ||
| template< class InputIt, class UnaryPredicate > bool all_of( InputIt first, InputIt last, UnaryPredicate p ); |
(C++11 èµ·) (C++20 å) |
|
| template< class InputIt, class UnaryPredicate > constexpr bool all_of( InputIt first, InputIt last, UnaryPredicate p ); |
(C++20 èµ·) | |
| template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > bool all_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p ); |
(2) | (C++17 èµ·) |
| (3) | ||
| template< class InputIt, class UnaryPredicate > bool any_of( InputIt first, InputIt last, UnaryPredicate p ); |
(C++11 èµ·) (C++20 å) |
|
| template< class InputIt, class UnaryPredicate > constexpr bool any_of( InputIt first, InputIt last, UnaryPredicate p ); |
(C++20 èµ·) | |
| template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > bool any_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p ); |
(4) | (C++17 èµ·) |
| (5) | ||
| template< class InputIt, class UnaryPredicate > bool none_of( InputIt first, InputIt last, UnaryPredicate p ); |
(C++11 èµ·) (C++20 å) |
|
| template< class InputIt, class UnaryPredicate > constexpr bool none_of( InputIt first, InputIt last, UnaryPredicate p ); |
(C++20 èµ·) | |
| template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > bool none_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p ); |
(6) | (C++17 èµ·) |
1) æ£æ¥ä¸å
è°è¯
p æ¯å¦å¯¹èå´ [first, last) 䏿æå
ç´ è¿å true ã3) æ£æ¥ä¸å
è°è¯
p æ¯å¦å¯¹èå´ [first, last) ä¸è³å°ä¸ä¸ªå
ç´ è¿å true ã5) æ£æ¥ä¸å
è°è¯
p æ¯å¦ä¸å¯¹èå´ [first, last) ä¸ä»»ä½å
ç´ è¿å true ã2,4,6) å (1,3,5) ï¼ä½æç
§
policy æ§è¡ãè¿äºéè½½ä»
è¥ std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true æåä¸éè½½å³è®®ãåæ°
| first, last | - | è¦æ£éªçå ç´ èå´ |
| policy | - | æç¨çæ§è¡çç¥ãç»èè§æ§è¡çç¥ã |
| p | - | ä¸å
è°è¯ã 对æ¯ä¸ªï¼å¯ä¸º const çï¼ |
| ç±»åè¦æ± | ||
-InputIt å¿
须满足éçè¾å
¥è¿ä»£å¨ (LegacyInputIterator) çè¦æ±ã
| ||
-ForwardIt å¿
须满足éçååè¿ä»£å¨ (LegacyForwardIterator) çè¦æ±ã
| ||
-UnaryPredicate å¿
须满足è°è¯ (Predicate) çè¦æ±ã
| ||
è¿åå¼
1-2) è¥ä¸å
è°è¯å¯¹èå´ä¸ææå
ç´ è¿å true å为 true ï¼å¦å为 false ãè¥èå´ä¸ºç©ºåè¿å true ã
3-4) è¥ä¸å
è°è¯å¯¹èå´ä¸è³å°ä¸ä¸ªå
ç´ è¿å true å为 true ï¼å¦å为 false ãè¥èå´ä¸ºç©ºåè¿å false ã
5-6) è¥ä¸å
è°è¯ä¸å¯¹èå´ä¸ä»»ä½å
ç´ è¿å true å为 true ï¼å¦å为 false ãè¥èå´ä¸ºç©ºåè¿å true ã
å¤æåº¦
1,3,5) è³å¤åºç¨è°è¯
last - first 次2,4,6) åºç¨è°è¯
O(last-first) 次å¼å¸¸
æ¥æå为 ExecutionPolicy çæ¨¡æ¿å½¢åçéè½½æä¸åæ¹å¼æ¥åé误ï¼
- è¥ä½ä¸ºç®æ³ä¸é¨åè°ç¨ç彿°çæ§è¡æåºå¼å¸¸ï¼ä¸
ExecutionPolicy为æ åçç¥ä¹ä¸ï¼åè°ç¨ std::terminate ã对äºä»»ä½å ¶ä»ExecutionPolicyï¼è¡ä¸ºæ¯å®ç°å®ä¹çã - è¥ç®æ³æ æ³åé å åï¼åæåº std::bad_alloc ã
å¯è½çå®ç°
| çæ¬ä¸ |
|---|
template< class InputIt, class UnaryPredicate > bool all_of(InputIt first, InputIt last, UnaryPredicate p) { return std::find_if_not(first, last, p) == last; } |
| çæ¬äº |
template< class InputIt, class UnaryPredicate > bool any_of(InputIt first, InputIt last, UnaryPredicate p) { return std::find_if(first, last, p) != last; } |
| çæ¬ä¸ |
template< class InputIt, class UnaryPredicate > bool none_of(InputIt first, InputIt last, UnaryPredicate p) { return std::find_if(first, last, p) == last; } |
示ä¾
è¿è¡æ¤ä»£ç
#include <vector> #include <numeric> #include <algorithm> #include <iterator> #include <iostream> #include <functional> Â int main() { std::vector<int> v(10, 2); std::partial_sum(v.cbegin(), v.cend(), v.begin()); std::cout << "Among the numbers: "; std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; Â if (std::all_of(v.cbegin(), v.cend(), [](int i){ return i % 2 == 0; })) { std::cout << "All numbers are even\n"; } if (std::none_of(v.cbegin(), v.cend(), 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 (std::any_of(v.cbegin(), v.cend(), 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