I have a need for a stack-like (LIFO) container. Feels like this is a common task, but cannot find any similar container to do the job. Expected something like TStack<>.
It should be possible to do this with a TArray. Using Add() to ‘push’ elements at the end of the Array, using Get() at last index in combination with RemoveAt() to ‘pop’ the last value.
TArray has Push and Pop methods that allow you to use it as stack instead of there being a unique container for that.
This would probably work but I feel I’m missing out on some c++ optimizations.
Didn’t know TArray has a Push and Pop. This is the best solution. I should mention that I found TDeque which would also work.
TDeque<int> Tester;
Tester.PushLast(1);
Tester.PushLast(2);
Tester.PushLast(3);
int Result = -1;
Tester.TryPopLast(Result); // 3
Tester.TryPopLast(Result); // 2
Tester.TryPopLast(Result); // 1
TArray<int> Tester2;
Tester2.Push(1);
Tester2.Push(2);
Tester2.Push(3);
Result = Tester2.Pop(); // 3
Result = Tester2.Pop(); // 2
Result = Tester2.Pop(); // 1
My only hesitation with TDeque is that templated types tend to be unsupported when it comes to UPROPERTY/reflection/blueprint. TArray, TMap & TSet are uniquely supported in that regard. But if you don’t need those things then it looks like a fine option.