当前位置:网站首页>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
边栏推荐
- Market Research - current market situation and future development trend of aircraft front wheel steering system
- LandingSite eBand B1冒烟测试用例
- 技术人创业:失败不是成功,但反思是
- The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
- Pointer array parameter passing, pointer parameter passing
- "Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
- Market Research - current situation and future development trend of marine clutch Market
- Promise optimized callback hell
- Web侧防御指南
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
猜你喜欢
Ransack combined condition search implementation
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
Perceptron model and Application
20220702 how do programmers build knowledge systems?
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
Micro service gateway selection, please accept my knees!
Infrastructure is code: a change is coming
Technical solution of vision and manipulator calibration system
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
随机推荐
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
Une semaine de vie
《Just because》阅读感受
New feature of go1.18: trylock, which has been tossed n times
Image segmentation using pixellib
The difference between include < > and include ""
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
Infrastructure is code: a change is coming
tinymce可视化编辑器增加百度地图插件
20220702 how do programmers build knowledge systems?
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
Market Research - current market situation and future development trend of marine wet exhaust hose
Pyqt picture decodes and encodes and loads pictures
*C language final course design * -- address book management system (complete project + source code + detailed notes)
[Jianzhi offer] 57 And are two numbers of S
Web侧防御指南
The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
技术人创业:失败不是成功,但反思是