RealDocs

TArray::HeapPop

function Core Since 4.0
#include "Containers/Array.h"
Access: public

Description

Removes and returns the top element (the maximum under the current predicate) from the heap, then re-heapifies the remaining elements. A predicate overload is available for custom ordering.

Caveats & Gotchas

  • Calling HeapPop on an empty array triggers a bounds check crash. Always verify Num() > 0 beforehand.
  • The bool bAllowShrinking overload is deprecated in UE 5.x — use EAllowShrinking::Yes/No instead.
  • After the call the element at index 0 becomes the new maximum, but the internal order of other elements is implementation-defined and changes with each pop.

Signature

void HeapPop(ElementType& OutItem, EAllowShrinking AllowShrinking = UE::Core::Private::AllowShrinkingByDefault<AllocatorType>())

Parameters

Name Type Description Default
OutItem ElementType& Receives the removed top (maximum) element.
AllowShrinking EAllowShrinking Controls whether the array's allocation can shrink after removal. Pass EAllowShrinking::No in hot loops to avoid reallocations. UE::Core::Private::AllowShrinkingByDefault<AllocatorType>()

Return Type

void

Example

Draining a max-heap in sorted order C++
TArray<int32> Heap = {3, 1, 4, 1, 5, 9, 2, 6};
Heap.Heapify();

while (Heap.Num() > 0)
{
    int32 Top;
    Heap.HeapPop(Top, EAllowShrinking::No);
    UE_LOG(LogTemp, Log, TEXT("%d"), Top); // prints 9, 6, 5, 4, 3, 2, 1, 1
}

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.