UE常用容器之TSet

TSet 是一种快速容器类,使用数据值本身作为键。

示例代码如下:

//=========================================================================================
//初始化
TSet FoodSet;

//=========================================================================================
//增
// 使用 Add 或 Emplace 添加元素。
FoodSet.Add(TEXT("Banana"));
FoodSet.Add(TEXT("Apple"));
FoodSet.Emplace(TEXT("Orange"));
// ==> FoodSet == [ "Banana", "Apple", "Orange" ]

// 使用 Append 函数来插入另一个集合中的所有元素。
// 存在 NewFoodSet == [ "Cake", "Cola", "Orange" ]
FoodSet.Append(NewFoodSet);
// ==> FoodSet == [ "Banana", "Apple", "Orange", "Cake", "Cola" ]

//=========================================================================================
//删
// 存在 FoodSet == [ "Banana", "Apple", "Orange", "Cake", "Cola" ]

// 使用 Remove 函数可按索引移除元素。
int RemovedCount = FoodSet.Remove(TEXT("Apple")); // ==> RemovedCount == 1
// ==> FoodSet == [ "Banana", , "Orange", "Cake", "Cola" ]

int RemovedCount = FoodSet.Remove(TEXT("Apple")); // ==> RemovedCount == 0
// ==> FoodSet == [ "Banana", , "Orange", "Cake", "Cola" ]

// 使用 Empty 或 Reset 函数可将集合中的所有元素移除。
FoodSet.Reset();
// ==> FoodSet == [ , , , ,  ]

FoodSet.Empty();
// ==> FoodSet == []

//=========================================================================================
//改
//排序
// 存在 FoodSet == [ "Apple", "Orange", "Cake", ]
FoodSet.Sort([](const FString& A, const FString& B) {
    return A > B;
});
// ==> FoodSet == [ "Apple", "Cake", "Orange"]

FoodSet.Sort([](const FString& A, const FString& B) {
    return A.Len() > B.Len();
});
// ==> FoodSet == [ "Orange", "Apple", "Cake"]

//内存
// 使用 Reserve 函数可直接创建 slack,在添加元素之前预分配内存。
TSet IntSet;
IntSet.Reserve(4); // ==> IntSet == [ , , ,  ]
// 使用 Shrink 函数可移除 TSet 中的全部 slack。
IntSet.Shrink(); // ==> IntSet == []

// 存在 FoodSet == [ "Apple", , "Orange", , "Cake", ]
// 使用 Compact 或 CompactStable 函数,将空白空间组合在一起,为调用 Shrink 做好准备。
FoodSet.Shrink(); // ==> IntSet == [ "Apple", , "Orange", , "Cake"]
FoodSet.Compact(); // ==> IntSet == [ "Apple", "Orange", "Cake", , ]
FoodSet.Shrink(); // ==> IntSet == [ "Apple", "Orange", "Cake" ]

//=========================================================================================
//查
// 存在 FoodSet == [ "Banana", "Apple", "Orange", "Cake", "Cola"]

// 使用 Num 函查询集合中保存的元素数量。
int32 Count = FoodSet.Num(); // ==> Count == 5

// 使用 Contains 函数查询是否包含特定元素。
bool bContains = FoodSet.Contains(TEXT("Banana")); // ==> bContains == true
bool bContains = FoodSet.Contains(TEXT("Water"));  // ==> bContains == false

// 使用 FSetElementId 结构体可查找集合中某个键的索引。
// 使用运算符 [] 查找元素。
FSetElementId Index = FoodSet.Index(TEXT("Banana")); // ==> FoodSet[Index] == TEXT("Banana")
FSetElementId Index = FoodSet.Index(TEXT("Water"));  // ==> FoodSet[Index] Assert

// 使用 Find 返回指向元素数值的指针。
FString* Ptr = FoodSet.Find(TEXT("Banana")); // ==> *Ptr == TEXT("Banana")
FString* Ptr = FoodSet.Find(TEXT("Water"));  // ==> Ptr == nullptr

// 使用 Array 函数会返回一个 TArray,其中填充了 TSet 中每个元素的一份副本。
TArray FoodArray = FoodSet.Array();
// FoodArray == [ "Banana","Apple","Orange","Cake","Cola" ]

//遍历
// auto遍历。
for (auto& Elem : FoodSet)
{
	Elem; // 遍历的值。
}

// 迭代器遍历。
// CreateIterator 返回拥有读写访问权限的迭代器,
// CreateConstIterator 返回拥有只读访问权限的迭代器
for (auto It = FoodSet.CreateConstIterator(); It; ++It)
{
    *it;	// 遍历的值。
}


//=========================================================================================
//运算
// 存在 FoodSetA == [ "Orange", "Apple", "Cake"]
// 存在 FoodSetB == [ "Banana", "Apple", "Cola"]

// 使用 Intersect 求交集。
TSet FoodSetC = FoodSetA.Intersect(FoodSetB); // ==> FoodSetC == [ "Apple" ]

// 使用 Union 求并集。
TSet FoodSetD = FoodSetA.Union(FoodSetB); // ==> FoodSetD == [ "Orange", "Apple", "Cake", "Banana", "Cola" ]

// 使用 Difference 求差集,返回 this set 不在 other set 里的集合。
TSet FoodSetE = FoodSetA.Difference(FoodSetB); // A不在B里 ==> FoodSetD == [ "Orange", "Cake" ]
TSet FoodSetF = FoodSetB.Difference(FoodSetA); // B不在A里 ==> FoodSetD == [ "Banana", "Cola" ]

// 存在 IntSetA == [ 1,2,3,4 ]
// 存在 IntSetB == [ 1,2,3,4,5,6,7,8,9 ]
// 使用 Includes 计算 this set 是否包含 other set。
bool Includes = IntSetA.Includes(IntSetB); // ==> Includes == false
bool Includes = IntSetB.Includes(IntSetA); // ==> Includes == true

你可能感兴趣的:(UE5学习笔记,C++,UE,C++)