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::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::ranges::all_of, std::ranges::any_of, std::ranges::none_of
- std::ranges::for_each
- std::ranges::count, std::ranges::count_if
- std::ranges::mismatch, std::ranges::mismatch_result
- std::ranges::find, std::ranges::find_if, std::ranges::find_if_not
- std::ranges::adjacent_find
- 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::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++ åèæå >ç®æ³åº >æå¶çº¦ç®æ³ (C++20 èµ·) > std::ranges::all_of, std::ranges::any_of, std::ranges::none_of
std::ranges::all_of, std::ranges::any_of, std::ranges::none_of
     class Proj = std::identity,
     std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
     std::indirect_unary_predicate<
       std::projected<ranges::iterator_t<R>,Proj>> Pred >
     class Proj = std::identity,
     std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
     std::indirect_unary_predicate<
       std::projected<ranges::iterator_t<R>,Proj>> Pred >
     class Proj = std::identity,
     std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
     std::indirect_unary_predicate<
       std::projected<ranges::iterator_t<R>,Proj>> Pred >
pred æ¯å¦å¯¹èå´ [first, last) ä¸çææå
ç´ ï¼ä»¥æå½± proj æå½±åï¼è¿å true ãpred æ¯å¦å¯¹èå´ [first, last) ä¸è³å°ä¸ä¸ªå
ç´ ï¼ä»¥æå½± proj æå½±åï¼è¿å true ãpred æ¯å¦ä¸å¯¹èå´ [first, last) ä¸ä»»ä½å
ç´ ï¼ä»¥æå½± proj æå½±åï¼è¿å true ãr 为æºèå´ï¼å¦å以 ranges::begin(r) 为 first 并以 ranges::end(r) 为 last ãæ¤é¡µé¢ä¸æè¿°çä»¿å½æ°å®ä½æ¯ niebloids ï¼å³ï¼
- è°ç¨å®ä»¬ä¸çä»»ä½ä¸è æ¶ä¸å¯æå®æ¾å¼æ¨¡æ¿å®åã
- å®ä»¬æ ä¸å¯¹åæ°ä¾èµæ¥æ¾å¯è§ã
- å¨é常æ é宿¥æ¾å¯»æ¾å½æ°è°ç¨è¿ç®ç¬¦å·¦ä¾§çåç§°æ¾å°å®ä»¬ä¹ä¸æ¶ï¼å®ç¦æ¢åæ°ä¾èµæ¥æ¾ã
å®é ä¸ï¼å®ä»¬è½ä»¥å½æ°å¯¹è±¡ï¼æä»¥æäºç¹æ®ç¼è¯å¨æ©å±å®ç°ã
åæ°
| first, last | - | è¦æ£éªçå ç´ èå´ |
| r | - | è¦æ£éªçå ç´ èå´ |
| pred | - | åºç¨å°æå½±åå ç´ çè°è¯ |
| proj | - | åºç¨å°å ç´ çæå½± |
è¿åå¼
i æç«å为 true ï¼å¦å为 false ãè¥èå´ä¸ºç©ºåè¿å true ãi æç«å为 true ï¼å¦å为 false ãè¥èå´ä¸ºç©ºåè¿å false ãi æç«å为 true ï¼å¦å为 false ãè¥èå´ä¸ºç©ºåè¿å true ãå¤æåº¦
è³å¤åºç¨ last - first 次è°è¯åæå½±ã
å¯è½çå®ç°
| çæ¬ä¸ |
|---|
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; |
| çæ¬äº |
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; |
| çæ¬ä¸ |
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 <vector> #include <numeric> #include <algorithm> #include <iterator> #include <iostream> #include <functional> Â namespace ranges = std::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, 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 (彿°æ¨¡æ¿) |