This article may be helpful for those who are familiar with standard containers and would like to learn Unreal-specific containers, or for those who already know containers but would like to explore them a little deeper.

Here is information about the following containers: TArray, TSet, TMap.

TArray

Let’s start with TArray. TArray is a dynamically sized array, which is similar to vector in the standard library.

The main purpose of TArray is to be able to easily (both memory and performance) iterate over its elements, using the traditional and range-based for loops.

Traditional for loop example
TArray<int32> IntArray{5, 10, 15, 20, 25};

for (int32 Index = 0; Index < IntArray.Num(); ++Index)
{
    UE_LOG(LogTemp, Log, TEXT("Traditional for loop iteration. Element value: %d"), IntArray[Index]);
}

Range-based for loop example
TArray<int32> IntArray{5, 10, 15, 20, 25};

for (int32 Element : IntArray)
{
    UE_LOG(LogTemp, Log, TEXT("Range-based for loop iteration. Element value: %d"), Element);
}

From an asymptomatic point of view, all TArray operations except for indexing take linear O(n) time.

TSet

TSet is a dynamically sized associative array that stores keys as values. It is similar to set in standard library. Internally, it uses a hash table approach to store keys and elements. That is, the real key is passed through a hash function that encodes it into a number.

Technically, TSet encapsulates a TSparseArray which allows hashed keys to be optimally stored as indexes.

As a consequence, this container is designed to be able to instantly (in a constant time O(1)) search for an element. However, the elements in this container are likely to have an inconsistent in-memory order. And coupled with using a hash function each time to access an element makes it not a recommended option for iterating over all elements.

Example of searching for an element by key
TSet<FString> StringSet{TEXT("First element"), TEXT("Second element"), TEXT("Third element")};

// The value will be true since there is a "Second element" element
bool bFound = StringSet.Contains(TEXT("Second element"));

TMap

TMap is also a dynamically sized associative array similar to TSet, but which elements are presented as key-value pairs.

It is implemented in a similar way as std::unordered_map and has a similar structure to a hash table, but the main difference is that not keys are hashed, but key-value pairs.

Technically, TMap encapsulates a TSet containing a TPair<KeyType, ValueType>.

Thus, all operations, except searching for an element by value, occur in constant time O(1).

Since this container works similarly to TSet, it is not recommended to use it when iterating over all elements for the same reasons.

Example of searching for an element by key
TMap<FString, float> StringFloatMap
{
	{TEXT("First element"), 0.1f},
	{TEXT("Second element"), 0.2f},
	{TEXT("Third element"), 0.3f}
};

// The value found will be 0.2
float FoundValue = *StringFloatMap.Find(TEXT("Second element"));

Example of searching for an element by value
TMap<FString, float> StringFloatMap
{
	{TEXT("First element"), 0.1f},
	{TEXT("Second element"), 0.2f},
	{TEXT("Third element"), 0.3f}
};

// The key found will be "Third element"
FString FoundKey = *StringFloatMap.FindKey(0.3f);