当前位置:网站首页>U++ 学习笔记 堆
U++ 学习笔记 堆
2022-07-02 22:01:00 【是秃头的兔子呀】
Heapify
任何现有的数组都可以通过调用该函数变成一个堆。这被重载以接受谓词或不接受谓词,其中非谓词版本将使用元素类型operator<
来确定排序:
TArray<int32> HeapArr;
for (int32 Val = 10; Val != 0; --Val)
{
HeapArr.Add(Val);
}
// HeapArr == [10,9,8,7,6,5,4,3,2,1]
HeapArr.Heapify();
// HeapArr == [1,2,4,3,6,5,8,10,7,9]
可以通过 HeapPush 函数将新元素添加到堆中,重新排序其他节点以维护堆
HeapArr.HeapPush(4);
// HeapArr == [1,2,4,3,4,5,8,10,7,9,6]
和函数用于从堆中删除顶部节点HeapPop
。HeapPopDiscard
两者的区别在于前者采用对元素类型的引用来返回顶部元素的副本,而后者只是简单地移除顶部节点而不以任何方式返回它。这两个函数都会对数组进行相同的更改,并且通过适当地重新排序其他元素来再次维护堆:
int32 TopNode;
HeapArr.HeapPop(TopNode);
// TopNode == 1
// HeapArr == [2,3,4,6,4,5,8,10,7,9]
HeapRemoveAt
将从给定索引处的数组中删除一个元素,然后重新排序元素以维护堆:
HeapArr.HeapRemoveAt(1);
// HeapArr == [2,4,4,6,9,5,8,10,7]
这些函数中的每一个,包括Heapify
,都可以采用可选的二元谓词来确定堆中节点元素的顺序。默认情况下,堆操作使用元素类型operator<
来确定顺序。使用自定义谓词时,对所有堆操作使用相同的谓词很重要。
最后,可以使用 来检查堆的顶部节点HeapTop
,而无需更改数组:
int32 Top = HeapArr.HeapTop();
// Top == 2
边栏推荐
- [sword finger offer] 56 - I. the number of numbers in the array
- New feature of go1.18: trylock, which has been tossed n times
- 技术人创业:失败不是成功,但反思是
- App page sharing password rails implementation
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
- 在beforeDestroy中销毁localStorage中的值无效
- Une semaine de vie
- Market Research - current situation and future development trend of marine clutch Market
- 20220702-程序员如何构建知识体系?
- 如何访问kubernetes API?
猜你喜欢
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
Technical solution of vision and manipulator calibration system
图像基础概念与YUV/RGB深入理解
[shutter] shutter resource file use (import resource pictures | use image resources)
Hanoi Tower problem
540. Single element in ordered array
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
LandingSite eBand B1冒烟测试用例
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
#include<>和#include“”的区别
随机推荐
Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision
Using emqx cloud to realize one machine one secret verification of IOT devices
图像基础概念与YUV/RGB深入理解
LightGBM原理及天文数据中的应用
C语言,实现三子棋小游戏
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
技术人创业:失败不是成功,但反思是
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
Blue Bridge Cup Eliminate last one (bit operation, code completion)
[zero foundation I] Navicat download link
Chargement de l'image pyqt après décodage et codage de l'image
Promise optimized callback hell
Daily book -- analyze the pain points of software automation from simple to deep
[shutter] shutter gesture interaction (small ball following the movement of fingers)
Market Research - current situation and future development trend of environmental friendly fireworks Market
Regular expression (2)
Servicemesh mainly solves three pain points
Oriental Aesthetics and software design
Official announcement! The golden decade of new programmers and developers was officially released
Attack and defense world PWN question: Echo