Keywords: UE4, C++ Container Notes

Types STL Boost Unreal
Dynamic Array std::vector boost::array TArray
Linked List std::forward_list boost::intrusive::slist TLinkedList
Double Linked List std::list TDoubleLinkedList
Hash Map std::unordered_map boost::unordered_map TMap
Queue std::queue boost::lockfree::queue TQueue
Double End Queue std::deque boost::deque
SPSC Queue boost::lockfree::spsc_queue TCircularQueue
Unreal TDoubleLinkedList

Examples:

TDoubleLinkedList<AActor*> ActorList;
ActorList.AddTail(pActor1);
ActorList.AddTail(pActor2);

for (TDoubleLinkedList<AActor*>::TIterator Itr(ActorList.GetHead()); Itr; ++Itr)
{
    if(*Itr == nullptr)
    {
        ActorList.RemoveNode(*Itr);
    }
}
TArray Emplace

Append:

TArray<AActor*> Array01 = {Actor01};
TArray<AActor*> Array02 = {Actor02, Actor03};
Array01.Append(Array02.GetData(), Array02.Num());

Emplace:

struct MyStruct
{
    AActor* Actor;
    int32 Count;
    MyStruct(const AActor* InActor, int32 InCount) : Actor(InActor), Count(InCount) { }
};

TArray<MyStruct> Test;
Test.Emplace(Actor01, 5);
TMap

safe way to iteration remove tmap:

TMap<FName, UObject*> TestMap;
for(TMap<FName, UObject*>::TIterator ItRemove = TestMap.CreateIterator(); ItRemove; ++ItRemove)
{
    // If I want to remove the current pair for whatever reason.
    ItRemove.RemoveCurrent();
}
Memory Allocator
  • FDefaultAllocator: The default allocator of Unreal container, allocate space on heap memory.
  • TNonRelocatableInlineAllocator: Allocate space on stack memory if element count is less than specified number, otherwise allocate space on heap memory and copy all other elements into it.
  • TInlineAllocator: Likes TNonRelocatableInlineAllocator, but TInlineAllocator gives you opportunity to specify where to allocate space (stack or heap, default is heap) if element count exceeds specified count.
  • TFixedAllocator: Allocate space on stack memory, if it runs out of space in the fixed memory portion, your code will cause an error.

Example:

//use default allocator TSizedDefaultAllocator, all elements was created in heap memory.
TArray<Shape*> MyShapeArray;

//use TInlineAllocator,the first 16 elements are saved in stack memory, 
//allocate space in heap memory if add the 17th element, 
//then copy these 17 elements into it, and destruct the old stack memory.
TArray<Shape*, TInlineAllocator<16>> MyShapeArray;

//Likes TInlineAllocator, but difference is that it also allocats space in stack memory while 
//add the 17th element. but when add the 33th element, it will allocate space in heap memory.
TArray<Shape*, TInlineAllocator<16, TInlineAllocator<32>>> MyShapeArray;

Reference:
Optimizing TArray Usage for Performance


Thoughts are the shadows of our feelings -- always darker, emptier and simpler. ― Friedrich Nietzsche