TMap, TArray, TSet containers in Unreal Engine 5 C++
Written on: 15 January 2025
TMap
TMap<int, FString> MyMap = {
{1, TEXT("Item 1")},
{2, TEXT("Item 2")},
{3, TEXT("Item 3")}
};
FString MyValue = MyMap[2];
FString* MyValuePtr = MyMap.Find(3);
MyMap[2] = TEXT("New Item 2");
MyMap.Add(4, TEXT("Item 4"));
MyMap.Remove(2);
MyMap.Empty();
bool bHasKey = MyMap.Contains(5);
int32 MyCount = MyMap.Num();
for (auto& Elem : MyMap) {
int Key = Elem.Key;
FString Value = *Elem.Value;
}
for (auto It = MyMap.CreateConstIterator(); It; ++It) {
int Key = It.Key();
FString Value = *It.Value();
}
TArray<int> MyMapKeys;
MyMap.GenerateKeyArray(MyMapKeys);
TArray<FString> MyMapValues;
MyMap.GenerateValueArray(MyMapValues);
TArray
TArray<FString> MyArray = {
TEXT("Item 0"),
TEXT("Item 1"),
TEXT("Item 2"),
TEXT("Item 3")
};
FString MyItem = MyArray[2];
int MyItemIndex = MyArray.Find(TEXT("Item 1"));
MyArray[2] = TEXT("New Item 2");
MyArray.Add(TEXT("Item 4"));
MyArray.Insert(TEXT("Item in middle"), 2);
MyArray.Remove(TEXT("Item 1"));
MyArray.RemoveSingle(TEXT("Item 2"));
MyArray.RemoveAt(2);
MyArray.Empty();
bool bHasItem = MyArray.Contains(TEXT("Somet"));
int MyCount = MyArray.Num();
for (auto& Elem : MyArray) {
}
for (int Index = 0; Index != MyArray.Num(); ++Index) {
auto Item = MyArray[Index];
}
MyArray.Sort([](const FString& A, const FString& B) {
return A.Len() < B.Len();
});
TSet
TSet<FString> MySet = {
TEXT("Item 1"),
TEXT("Item 2"),
TEXT("Item 3"),
TEXT("Item 4"),
};
MySet.Add(TEXT("Item 5"));
MySet.Remove(TEXT("Item 2"));
MySet.Empty();
bool bHasItem = MySet.Contains(TEXT("Item 2"));
int MyCount = MySet.Num();
for (auto& Elem : MySet) {
}
for (auto It = MySet.CreateConstIterator(); It; ++It) {
auto Item = *It;
}