Description
Inserts an element into the array while maintaining max-heap order, sifting the new element up to its correct position. Returns the final index of the inserted element. A predicate overload exists for custom ordering.
Caveats & Gotchas
- • The array must already be in valid heap order before calling HeapPush. If not, call Heapify() first — pushing into an unheapified array silently produces a broken heap.
- • Arrays of raw pointers are automatically dereferenced by the internal predicate wrapper, so your comparator receives object references, not pointer values. Smart pointers do NOT receive this treatment and are compared by pointer address by default.
- • The returned index is the element's final position after sifting up and is not guaranteed to be at the end of the array.
Signature
SizeType HeapPush(ElementType&& InItem) Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| InItem | ElementType&& | Item to insert into the heap. | — |
Return Type
SizeType Example
Building a max-heap with HeapPush C++
TArray<int32> Heap;
Heap.Heapify(); // ensure heap property on empty array
Heap.HeapPush(10);
Heap.HeapPush(42);
Heap.HeapPush(7);
// Heap[0] == 42 (the max element)
// Custom ordering (min-heap):
Heap.HeapPush(5, TGreater<int32>()); Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?