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::make_heap
std::make_heap
| å®ä¹äºå¤´æä»¶ <algorithm>
|
||
| (1) | ||
| template< class RandomIt > void make_heap( RandomIt first, RandomIt last ); |
(C++20 å) | |
| template< class RandomIt > constexpr void make_heap( RandomIt first, RandomIt last ); |
(C++20 èµ·) | |
| (2) | ||
| template< class RandomIt, class Compare > void make_heap( RandomIt first, RandomIt last, |
(C++20 å) | |
| template< class RandomIt, class Compare > constexpr void make_heap( RandomIt first, RandomIt last, |
(C++20 èµ·) | |
å¨èå´ [first, last) ä¸æé æå¤§å ã彿°ç¬¬ä¸çæ¬ç¨ operator< æ¯è¾å
ç´ ï¼ç¬¬äºçæ¬ç¨ç»å®çæ¯è¾å½æ° comp ã
åæ°
| first, last | - | å¶ä½å æ¥æºçå ç´ èå´ |
| comp | - | æ¯è¾å½æ°å¯¹è±¡ï¼å³æ»¡è¶³æ¯è¾ (Compare) è¦æ±ç对象ï¼ï¼è¥é¦ä¸ªåæ°å°äºç¬¬äºä¸ªï¼åè¿å âtrue ã æ¯è¾å½æ°çç¾ååºçä»·äºå¦ä¸ï¼  bool cmp(const Type1 &a, const Type2 &b); è½ç¶ç¾åä¸å¿
æ const & ï¼å½æ°ä¹ä¸è½ä¿®æ¹ä¼ éç»å®ç对象ï¼èä¸å¿
é¡»æ¥åï¼å¯ä¸º const çï¼ç±»å |
| ç±»åè¦æ± | ||
-RandomIt å¿
须满足éçéæºè®¿é®è¿ä»£å¨ (LegacyRandomAccessIterator) çè¦æ±ã
| ||
-è§£å¼ç¨ RandomIt ç»æçç±»åå¿
须满足å¯ç§»å¨èµå¼ (MoveAssignable) åå¯ç§»å¨æé (MoveConstructible) çè¦æ±ã
| ||
è¿åå¼
ï¼æ ï¼
å¤æåº¦
è³å¤ 3*std::distance(first, last) 次æ¯è¾ã
注æ
æå¤§å æ¯æ¥æä¸å屿§çå
ç´ èå´ [f,l) ï¼
- 设
N = l - fï¼å¯¹äºææ0 < i < Nï¼f[floor(ä¸å°äº
)]i-1 2 f[i]ã - å¯ç¨ std::push_heap() æ·»å æ°å ç´
- å¯ç¨ std::pop_heap() ç§»é¤é¦å ç´
- 设
示ä¾
#include <iostream> #include <algorithm> #include <functional> #include <vector> Â int main() { std::cout << "Max heap:\n"; Â std::vector<int> v { 3, 2, 4, 1, 5, 9 }; Â std::cout << "initially, v: "; for (auto i : v) std::cout << i << ' '; std::cout << '\n'; Â std::make_heap(v.begin(), v.end()); Â std::cout << "after make_heap, v: "; for (auto i : v) std::cout << i << ' '; std::cout << '\n'; Â std::pop_heap(v.begin(), v.end()); Â std::cout << "after pop_heap, v: "; for (auto i : v) std::cout << i << ' '; std::cout << '\n'; Â auto top = v.back(); v.pop_back(); std::cout << "former top element: " << top << '\n'; Â std::cout << "after removing the former top element, v: "; for (auto i : v) std::cout << i << ' '; std::cout << '\n' << '\n'; Â std::cout << "Min heap:\n"; Â std::vector<int> v1 { 3, 2, 4, 1, 5, 9 }; Â std::cout << "initially, v1: "; for (auto i : v1) std::cout << i << ' '; std::cout << '\n'; Â std::make_heap(v1.begin(), v1.end(), std::greater<>{}); Â std::cout << "after make_heap, v1: "; for (auto i : v1) std::cout << i << ' '; std::cout << '\n'; Â std::pop_heap(v1.begin(), v1.end(), std::greater<>{}); Â std::cout << "after pop_heap, v1: "; for (auto i : v1) std::cout << i << ' '; std::cout << '\n'; Â auto top1 = v1.back(); v1.pop_back(); std::cout << "former top element: " << top1 << '\n'; Â std::cout << "after removing the former top element, v1: "; for (auto i : v1) std::cout << i << ' '; std::cout << '\n'; }
è¾åºï¼
Max heap: initially, v: 3 2 4 1 5 9 after make_heap, v: 9 5 4 1 2 3 after pop_heap, v: 5 3 4 1 2 9 former top element: 9 after removing the former top element, v: 5 3 4 1 2 Â Min heap: initially, v1: 3 2 4 1 5 9 after make_heap, v1: 1 2 4 3 5 9 after pop_heap, v1: 2 3 4 9 5 1 former top element: 1 after removing the former top element, v1: 2 3 4 9 5
åé
| å°ä¸ä¸ªæå¤§å åæä¸ä¸ªæååºæåºçå
ç´ èå´ (彿°æ¨¡æ¿) | |
| éé
ä¸ä¸ªå®¹å¨ä»¥æä¾ä¼å
级éå (类模æ¿) | |
| å®ç° x > y ç彿°å¯¹è±¡ (类模æ¿) |