# Analogue of priority queue

**URL:** <https://forums.unrealengine.com/t/analogue-of-priority-queue/308540>\
**Category:** C++\
**Tags:** container, unreal-engine, question, CPP\
**Created:** [February 28, 2015, 8:16pm UTC](https://forums.unrealengine.com/t/analogue-of-priority-queue/308540 "2015-02-28T20:16:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MikhailMaximov\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MikhailMaximov\_1](https://forums.unrealengine.com/u/MikhailMaximov_1)\
**Post date:** [February 28, 2015, 8:16pm UTC](https://forums.unrealengine.com/t/analogue-of-priority-queue/308540/1 "2015-02-28T20:16:37Z")

</div>

Question is very simple: is there analogue of std::priority\_queue in UE4?

---

<div class="post-metadata">

**Author:** ![Steve\_Robb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steve\_Robb](https://forums.unrealengine.com/u/Steve_Robb)\
**Post date:** [March 2, 2015, 12:03pm UTC](https://forums.unrealengine.com/t/analogue-of-priority-queue/308540/2 "2015-03-02T12:03:17Z")

</div>

Hi,

There isn’t a direct analogue to std::priority\_queue, but you can use heap functions on TArray instead, with an inverted predicate:

```
struct MyType
{
    explicit MyType(int32 InPriority);

    int32 Priority;
};

struct MyTypePredicate
{
    bool operator()(const MyType& A, const MyType& B) const
    {
        // Inverted compared to std::priority_queue - higher priorities float to the top
        return A.Priority > B.Priority;
    }
};

TArray<MyType> Queue;
Queue.HeapPush(MyType(10), MyTypePredicate());
Queue.HeapPush(MyType(5), MyTypePredicate());
Queue.HeapPush(MyType(20), MyTypePredicate());
Queue.HeapPush(MyType(15), MyTypePredicate());

MyType Next;
Queue.HeapPop(Next, MyTypePredicate());
check(Next.Priority == 20);

```

Hope this helps,

Steve

---

<div class="post-metadata">

**Author:** ![MikhailMaximov\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MikhailMaximov\_1](https://forums.unrealengine.com/u/MikhailMaximov_1)\
**Post date:** [March 2, 2015, 7:39pm UTC](https://forums.unrealengine.com/t/analogue-of-priority-queue/308540/3 "2015-03-02T19:39:45Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Horny-Squirrel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Horny-Squirrel](https://forums.unrealengine.com/u/Horny-Squirrel)\
**Post date:** [June 22, 2017, 2:08pm UTC](https://forums.unrealengine.com/t/analogue-of-priority-queue/308540/4 "2017-06-22T14:08:13Z")

</div>

thanks for this

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_576f2c0a1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_576f2c0a1](https://forums.unrealengine.com/u/anonymous_user_576f2c0a1)\
**Post date:** [September 15, 2018, 3:19pm UTC](https://forums.unrealengine.com/t/analogue-of-priority-queue/308540/5 "2018-09-15T15:19:18Z")

</div>

Nice solution!! but i have one question is this thread safe for a scenario of one consumer and many producers?

---

<div class="post-metadata">

**Author:** ![Steve\_Robb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steve\_Robb](https://forums.unrealengine.com/u/Steve_Robb)\
**Post date:** [September 17, 2018, 7:52am UTC](https://forums.unrealengine.com/t/analogue-of-priority-queue/308540/6 "2018-09-17T07:52:32Z")

</div>

I’m afraid not. You’ll need to protect access to the array by a mutex.

Steve

---

<div class="post-metadata">

**Author:** ![radicalraid](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@radicalraid](https://forums.unrealengine.com/u/radicalraid)\
**Post date:** [January 12, 2021, 1:05am UTC](https://forums.unrealengine.com/t/analogue-of-priority-queue/308540/7 "2021-01-12T01:05:51Z")

</div>

For people finding this now, there’s (in my opinion) a better way to do it via overriding the \< operator and wrapping a TArray up a bit.

Keep in mind: _A lower number means higher priority_

**CODE**

```
template <typename InElementType>
struct TPriorityQueueNode {
    InElementType Element;
    float Priority;

    TPriorityQueueNode()
    {
    }

    TPriorityQueueNode(InElementType InElement, float InPriority)
    {
        Element = InElement;
        Priority = InPriority;
    }

    bool operator<(const TPriorityQueueNode<InElementType> Other) const
    {
        return Priority < Other.Priority;
    }
};

template <typename InElementType>
class TPriorityQueue {
public:
    TPriorityQueue()
    {
        Array.Heapify();
    }

public:
    // Always check if IsEmpty() before Pop-ing!
    InElementType Pop()
    {
        TPriorityQueueNode<InElementType> Node;
        Array.HeapPop(Node);
        return Node.Element;
    }

    TPriorityQueueNode<InElementType> PopNode()
    {
        TPriorityQueueNode<InElementType> Node;
        Array.HeapPop(Node);
        return Node;
    }

    void Push(InElementType Element, float Priority)
    {
        Array.HeapPush(TPriorityQueueNode<InElementType>(Element, Priority));
    }

    bool IsEmpty() const
    {
        return Array.Num() == 0;
    }

private:
    TArray<TPriorityQueueNode<InElementType>> Array;
};

```

**USAGE**

```
// -- Construction
TPriorityQueue<UTile*> Frontier;

// -- Adding elements: (element, priority)
Frontier.Push(TargetTile, 0.0f);
Frontier.Push(Grid->At(10, 16), 8.0f);
Frontier.Push(StartTile, 1000.0f);
     
// -- Iteration
while (!Frontier.IsEmpty()) {
    UTile* Current = Frontier.Pop(); // Removes it from the top of the heap
    Current->DoMagic();
}
```
