当前位置:网站首页>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
边栏推荐
- ArrayList analysis 2: pits in ITR, listiterator, and sublist
- VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
- Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source
- [Jianzhi offer] 57 And are two numbers of S
- 540. Single element in ordered array
- Share how to make professional hand drawn electronic maps
- GEE:(二)对影像进行重采样
- Market Research - current market situation and future development trend of aircraft front wheel steering system
- Etcd raft protocol
- An overview of the development of affective computing and understanding research
猜你喜欢
"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!
From personal heroes to versatile developers, the era of programmer 3.0 is coming
Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source
情感计算与理解研究发展概述
图像基础概念与YUV/RGB深入理解
Reading experience of just because
Error in PIP installation WHL file: error: is not a supported wheel on this platform
20220702-程序员如何构建知识体系?
[001] [arm-cortex-m3/4] internal register
Share how to make professional hand drawn electronic maps
随机推荐
[shutter] shutter application theme (themedata | dynamic modification theme)
Market Research - current market situation and future development trend of high tibial osteotomy plate
The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
Market Research - current situation and future development trend of marine clutch Market
What is it that makes you tremble? Those without fans can learn
加了定位的文字如何水平垂直居中
[leetcode] sword finger offer 11 Rotate the minimum number of the array
New feature of go1.18: introduce new netip Network Library
Daily book -- analyze the pain points of software automation from simple to deep
地理探测器原理介绍
Necessary browser plug-ins for network security engineers
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
Leetcode theme [array] -169- most elements
The failure rate is as high as 80%. What should we do about digital transformation?
Pointer and string
The difference between include < > and include ""
Web侧防御指南
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
Reading experience of just because
图像基础概念与YUV/RGB深入理解