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::adjacent_difference
std::adjacent_difference
OutputIt adjacent_difference( InputIt first, InputIt last,
constexpr OutputIt adjacent_difference( InputIt first, InputIt last,
ForwardIt2 adjacent_difference( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,
OutputIt adjacent_difference( InputIt first, InputIt last,
constexpr OutputIt adjacent_difference( InputIt first, InputIt last,
ForwardIt2 adjacent_difference( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,
è®¡ç® [first, last) èå´ä¸æ¯å¯¹ç¸é»å
ç´ ç第äºä¸ªå第ä¸ä¸ªçå·®ï¼å¹¶åå
¥å®ä»¬å°å§äº d_first + 1 çèå´ãåå
¥ä¸ä¿®æ¹ç *first 坿¬å° *d_first ã
InputIt ç value_type çç§¯ç´¯å¨ acc ï¼ä»¥ *first åå§åï¼å¹¶èµå¼ä¸º *d_first çç»æãç¶åï¼å¯¹äº [first + 1, last) ä¸æé¡ºåºçæ¯ä¸ªè¿ä»£å¨ i ï¼å建类å为 InputIt ç value_type ç对象 val ï¼ä»¥ *i åå§åï¼è®¡ç® val - acc (C++20 å)val - std::move(acc) (C++20 èµ·) ï¼éè½½ (1) ï¼æ op(val, acc) (C++20 å)op(val, std::move(acc)) (C++20 èµ·) ï¼éè½½ (3) ï¼ï¼èµå¼ç»æå° *(d_first + (i - first)) ï¼å¹¶ä» val ç§»å¨èµå¼å° acc ã[1, last - first - 1] ä¸ç d ï¼èµ *(first + d) - *(first + d - 1) ï¼éè½½ (2) ï¼æ op(*(first + d), *(first + d - 1)) ï¼éè½½ (4) ï¼ç» *(d_first + d) ãæç
§ policy æ§è¡ä¹ãæ¤éè½½ä»
è¥ std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true æåä¸éè½½å³è®®ã
çä»·æä½ï¼
*(d_first) = *first; *(d_first+1) = *(first+1) - *(first); *(d_first+2) = *(first+2) - *(first+1); *(d_first+3) = *(first+3) - *(first+2); ...
|
|
(C++11 å) |
|
|
(C++11 èµ·) |
åæ°
| first, last | - | å ç´ èå´ |
| d_first | - | ç®æ èå´çèµ·å§ |
| policy | - | æç¨çæ§è¡çç¥ãç»èè§æ§è¡çç¥ã |
| op | - | 被使ç¨çäºå
彿°å¯¹è±¡ã è¯¥å½æ°çç¾ååºå½çä»·äºï¼  Ret fun(const Type1 &a, const Type2 &b); ç¾åä¸å¹¶ä¸éè¦æ const &ã |
| ç±»åè¦æ± | ||
-InputIt å¿
须满足éçè¾å
¥è¿ä»£å¨ (LegacyInputIterator) çè¦æ±ãInputIt çå¼ç±»åå¿
é¡»å¯ç§»å¨èµå¼ (MoveAssignable) ä¸å¯ä» *first çç±»åæé
| ||
-OutputIt å¿
须满足éçè¾åºè¿ä»£å¨ (LegacyOutputIterator) çè¦æ±ãacc ï¼ç§¯ç´¯çå¼ï¼å val - acc æ op(val, acc) (C++20 å)val - std::move(acc) æ op(val, std::move(acc)) (C++20 èµ·) ç»æé½å¿
é¡»å¯åå
¥ OutputIt
| ||
-ForwardIt1, ForwardIt2 å¿
须满足éçååè¿ä»£å¨ (LegacyForwardIterator) çè¦æ±ã*first ã *first - *first ï¼å¯¹äº (2) ï¼å op(*first, *first) ï¼å¯¹äº (4) ï¼çç»æå¿
é¡»å¯åå
¥ ForwardIt2 ã
| ||
è¿åå¼
æåæå被åå ¥å ç´ åä¸ä½ç½®çè¿ä»£å¨ã
注解
è¥ first == last ï¼åæ¤å½æ°æ ææå¹¶ä¸åªä¼è¿å d_first ã
å¤æåº¦
æ°å¥½åºç¨ (last - first) - 1 次äºå
è¿ç®ã
å¼å¸¸
æ¥æå为 ExecutionPolicy çæ¨¡æ¿å½¢åçéè½½æä¸åæ¹å¼æ¥åé误ï¼
- è¥ä½ä¸ºç®æ³ä¸é¨åè°ç¨ç彿°çæ§è¡æåºå¼å¸¸ï¼ä¸
ExecutionPolicy为æ åçç¥ä¹ä¸ï¼åè°ç¨ std::terminate ã对äºä»»ä½å ¶ä»ExecutionPolicyï¼è¡ä¸ºæ¯å®ç°å®ä¹çã - è¥ç®æ³æ æ³åé å åï¼åæåº std::bad_alloc ã
å¯è½çå®ç°
| çæ¬ä¸ |
|---|
template<class InputIt, class OutputIt> constexpr // C++20 èµ· OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first) { if (first == last) return d_first; Â typedef typename std::iterator_traits<InputIt>::value_type value_t; value_t acc = *first; *d_first = acc; while (++first != last) { value_t val = *first; *++d_first = val - std::move(acc); // C++20 èµ·æ std::move acc = std::move(val); } return ++d_first; } |
| çæ¬äº |
template<class InputIt, class OutputIt, class BinaryOperation> constexpr // C++20 èµ· OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first, BinaryOperation op) { if (first == last) return d_first; Â typedef typename std::iterator_traits<InputIt>::value_type value_t; value_t acc = *first; *d_first = acc; while (++first != last) { value_t val = *first; *++d_first = op(val, std::move(acc)); // C++20 èµ·æ std::move acc = std::move(val); } return ++d_first; } |
示ä¾
#include <numeric> #include <vector> #include <iostream> #include <functional>  int main() { // é»è®¤å®ç°ââäºä¸ªé¡¹ä¹é´çå·®  std::vector<int> v{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; std::adjacent_difference(v.begin(), v.end(), v.begin());  for (auto n : v) { std::cout << n << ' '; } std::cout << '\n';  // ææ³¢é£å¥ // 注æï¼å表ä¸ä¸ä¸é¡¹æ¯å½åè¿ä»£çç»æ  v = std::vector<int>(10); v[0] = 1;  std::adjacent_difference(v.begin(), v.end() - 1, v.begin() + 1, std::plus<int>());  for (auto n : v) { std::cout << n << ' '; } std::cout << '\n'; }
è¾åºï¼
2 2 2 2 2 2 2 2 2 2 1 1 2 3 5 8 13 21 34 55
åé
| 计ç®èå´å
å
ç´ çé¨åå (彿°æ¨¡æ¿) | |
| 对ä¸ä¸ªèå´å
çå
ç´ æ±å (彿°æ¨¡æ¿) |