Fork me on GitHub

关于 UE4 TMap

1. 前一篇关于 通过 Key 查找 Value 的部分:

2. 通过 Value 查找 Key

FindKey(Value)
1     auto ValFindKey = [](const TMap<int32, FString> MapType, FString Value)->void
2     {
3         const int32* ShowFindKey = MapType.FindKey(Value);
4         UE_LOG(LogTemp, Display, TEXT("ValFindKey    | Val: %s ,Key: %d"), *Value, *ShowFindKey);
5         UE_LOG(LogTemp, Display, TEXT("============================="));
6     };

 

3. 查找 Key,找不到则新增

Map1.FindOrAdd(1);
1     // 先查找,如果找不到则新增 Key, Value则是空的,返回Value的引用
2     FString& FVal = Map1.FindOrAdd(1);
3     FVal = TEXT("BBB");
4     KeyFindVal_1(Map1, 1);

 

 

4.  遍历 TMap

4.1 使用 foreach 的方式遍历 TMap

1     auto ForEachMap = [](const TMap<int32, FString>& Valu)->void
2     {
3         for (const TPair<int32, FString>& element : Valu)
4         {
5             UE_LOG(LogTemp, Display, TEXT("ForEachMap | Key: %d, Value: %s "), element.Key, *(element.Value));
6         }
7         UE_LOG(LogTemp, Display, TEXT("============================="));
8     };

 

4.2 使用迭代器的方式遍历 TMap

1     auto ForItMap = [](const TMap<int32, FString>& Valu)->void
2     {
3         for (TMap<int32, FString>::TConstIterator it = Valu.CreateConstIterator(); it; ++it)
4         {
5             UE_LOG(LogTemp, Display, TEXT("ForItMap   | Key: %d, Value: %s "), it.Key(), *(it.Value()));
6         }
7         UE_LOG(LogTemp, Display, TEXT("============================="));
8     };

 

5. 显示当前 Map 所有的 Key

Map1.GenerateKeyArray(KeyList);
 1     // 显示当前 Map 所有的 Key
 2     TArray<int32> KeyList;
 3     Map1.GenerateKeyArray(KeyList);
 4     if (KeyList.Num() > 0)
 5     {
 6         for (int32& KeyNode : KeyList)
 7         {
 8             UE_LOG(LogTemp, Display, TEXT("Key: %d"), KeyNode);
 9         }
10         UE_LOG(LogTemp, Display, TEXT("============================="));
11     }

 

6. 显示当前 Map 所有的 Value

Map1.GenerateValueArray(ValList);
 1     // 显示当前 Map 所有的 Value
 2     TArray <FString> ValList;
 3     Map1.GenerateValueArray(ValList);
 4     if (ValList.Num() > 0)
 5     {
 6         for (FString& ValNode : ValList)
 7         {
 8             UE_LOG(LogTemp, Display, TEXT("Val: %s"), *ValNode);
 9         }
10         UE_LOG(LogTemp, Display, TEXT("============================="));
11     }

 

7.  两个 Map 合并(相同 Key 时会覆盖旧的)

1     Map1.Append(Map2);

 

 

8. 删除指定 Key 以及元素,返回与该 Key 关联元素的个数

Map1.Remove(0)
1     // 删除指定 Key 的元素,返回与该 Key 关联值的个数
2     if (Map1.Contains(0))
3     {
4         int32 RemoveValNum = Map1.Remove(0);
5         UE_LOG(LogTemp, Display, TEXT("RemoveValNum: %d"), RemoveValNum);
6         ForItMap(Map1);
7     }

 

 

9. 删除指定 Key 的元素,并返回删掉的元素内容

  如果元素存在则会返回被删除内容的深拷贝。但会出现没有找到对应 Key 以至于抛出异常,这时候去使用他的返回值会导致内存崩掉
  Map1.FindAndRemoveChecked(1);
1     // 删除指定 Key 的元素,如果元素存在则会返回被删除内容的深拷贝。但会出现没有找到对应 Key 以至于抛出异常,这时候去使用他的返回值会导致内存崩掉
2     if (Map1.Contains(1))
3     {
4         FString FindAndRemoveString = Map1.FindAndRemoveChecked(1);
5         UE_LOG(LogTemp, Display, TEXT("FindAndRemoveString: %s"), *FindAndRemoveString);
6         ForItMap(Map1);
7     }

 

 

10. 删除指定的 Key 的元素,并将删掉的内容赋值给相同类型

  如果元素存在则会将被删除元素的值赋值给传入的第二个参数,删除成功返回 True, 否则返回 False

  Map1.RemoveAndCopyValue(1, RemoveAndCoryFString)
1     // 删除指定的 Key 的元素,如果元素存在则会将被删除元素的值赋值给传入的第二个参数,删除成功返回 True, 否则返回 False
2     FString RemoveAndCoryFString;
3     if (Map1.RemoveAndCopyValue(1, RemoveAndCoryFString))
4     {
5         UE_LOG(LogTemp, Display, TEXT("RemoveAndCoryFString: %s"), *RemoveAndCoryFString);
6         ForItMap(Map1);
7     }

 

 

11. 清空 Map

1     // 清空 Map
2     Map1.Empty();

 

 

测试结果:

  

 

 

关于 TMap 的增删改查的全部代码

  1 // Called when the game starts or when spawned
  2 void ATMapActor::BeginPlay()
  3 {
  4     Super::BeginPlay();
  5 
  6     // 通过 Key 查找 value;  Contains 方法
  7     auto KeyFindVal_1 = [](const TMap<int32, FString> Value, int32 Key)->void
  8     {
  9         if (Value.Contains(Key))
 10         {
 11             UE_LOG(LogTemp, Display, TEXT("Key is %d value is: %s"), Key, *Value[Key]);
 12             UE_LOG(LogTemp, Display, TEXT("============================="));
 13         }
 14         else
 15         {
 16             UE_LOG(LogTemp, Display, TEXT("Key %d is error"), Key);
 17             UE_LOG(LogTemp, Display, TEXT("============================="));
 18         }
 19     };
 20 
 21     // 通过 Find 查找 value; Find 方法
 22     auto KeyFindVal_2 = [](const TMap<int32, FString> Value, int32 Key)->void
 23     {
 24         if (Value.Contains(Key))
 25         {
 26             const FString* Vall = Value.Find(Key);
 27             if (Vall != nullptr)
 28             {
 29                 UE_LOG(LogTemp, Display, TEXT("Key is %d value is: %s"), Key, Vall->operator*());
 30                 UE_LOG(LogTemp, Display, TEXT("============================="));
 31             }
 32             else
 33             {
 34                 UE_LOG(LogTemp, Display, TEXT("Key %d Ptr error"), Key);
 35                 UE_LOG(LogTemp, Display, TEXT("============================="));
 36             }
 37         }
 38         else
 39         {
 40             UE_LOG(LogTemp, Display, TEXT("Key %d is error"), Key);
 41             UE_LOG(LogTemp, Display, TEXT("============================="));
 42         }
 43     };
 44 
 45     // 通过 FindRef 查找 value; FindRef 方法
 46     auto KeyFindVal_3 = [](const TMap<int32, FString> Value, int32 Key)->void
 47     {
 48         if (Value.Contains(Key))
 49         {
 50             const FString Vall = Value.FindRef(Key);
 51             if (&Vall != nullptr)
 52             {
 53                 // UE_LOG(LogTemp, Display, TEXT("Key is %d value is: %s"), Key, *Vall);     两种方式都可以
 54                 UE_LOG(LogTemp, Display, TEXT("Key is %d value is: %s"), Key, Vall.operator*());
 55                 UE_LOG(LogTemp, Display, TEXT("============================="));
 56             }
 57             else
 58             {
 59                 UE_LOG(LogTemp, Display, TEXT("Key %d Ptr error"), Key);
 60                 UE_LOG(LogTemp, Display, TEXT("============================="));
 61             }
 62         }
 63         else
 64         {
 65             UE_LOG(LogTemp, Display, TEXT("Key %d is error"), Key);
 66             UE_LOG(LogTemp, Display, TEXT("============================="));
 67         }
 68     };
 69 
 70     // 通过 Value 查找 Key
 71     auto ValFindKey = [](const TMap<int32, FString> MapType, FString Value)->void
 72     {
 73         const int32* ShowFindKey = MapType.FindKey(Value);
 74         UE_LOG(LogTemp, Display, TEXT("ValFindKey    | Val: %s ,Key: %d"), *Value, *ShowFindKey);
 75         UE_LOG(LogTemp, Display, TEXT("============================="));
 76     };
 77 
 78     // 遍历 TMap
 79     auto ForEachMap = [](const TMap<int32, FString>& Valu)->void
 80     {
 81         for (const TPair<int32, FString>& element : Valu)
 82         {
 83             UE_LOG(LogTemp, Display, TEXT("ForEachMap | Key: %d, Value: %s "), element.Key, *(element.Value));
 84         }
 85         UE_LOG(LogTemp, Display, TEXT("============================="));
 86     };
 87 
 88     // 使用迭代器的方式遍历 TMap
 89     auto ForItMap = [](const TMap<int32, FString>& Valu)->void
 90     {
 91         for (TMap<int32, FString>::TConstIterator it = Valu.CreateConstIterator(); it; ++it)
 92         {
 93             UE_LOG(LogTemp, Display, TEXT("ForItMap   | Key: %d, Value: %s "), it.Key(), *(it.Value()));
 94         }
 95         UE_LOG(LogTemp, Display, TEXT("============================="));
 96     };
 97 
 98     TMap<int32, FString> Map1;
 99     TMap<int32, FString> Map2;
100     Map2.Add(0, TEXT("aaa"));
101     Map2.Add(1, TEXT("bbb"));
102     Map2.Add(2, TEXT("ccc"));
103     Map2.Add(3, TEXT("ddd"));
104 
105     // 添加元素
106     Map1.Add(0, TEXT("AAA"));
107     Map1.Add(1, TEXT("bbb"));
108     Map1.Add(2, TEXT("CCC"));
109     Map1.Add(3, TEXT("DDD"));
110     Map1.Add(4, TEXT("EEE"));
111     Map1.Add(5, TEXT("FFF"));
112 
113     KeyFindVal_1(Map1, 0);
114 
115     // 先查找,如果找不到则新增 Key, Value则是空的,返回Value的引用
116     FString& FVal = Map1.FindOrAdd(1);
117     FVal = TEXT("BBB");
118     KeyFindVal_1(Map1, 1);
119 
120     ValFindKey(Map1, TEXT("BBB"));
121 
122 
123     // 显示当前 Map 所有的 Key
124     TArray<int32> KeyList;
125     Map1.GenerateKeyArray(KeyList);
126     if (KeyList.Num() > 0)
127     {
128         for (int32& KeyNode : KeyList)
129         {
130             UE_LOG(LogTemp, Display, TEXT("Key: %d"), KeyNode);
131         }
132         UE_LOG(LogTemp, Display, TEXT("============================="));
133     }
134 
135     // 显示当前 Map 所有的 Value
136     TArray <FString> ValList;
137     Map1.GenerateValueArray(ValList);
138     if (ValList.Num() > 0)
139     {
140         for (FString& ValNode : ValList)
141         {
142             UE_LOG(LogTemp, Display, TEXT("Val: %s"), *ValNode);
143         }
144         UE_LOG(LogTemp, Display, TEXT("============================="));
145     }
146 
147     // 两个 Map 合并,相同 Key 时以参数 Map 的值为准
148     Map1.Append(Map2);
149     ForEachMap(Map1);
150 
151     // 删除指定 Key 的元素,返回与该 Key 关联值的个数
152     if (Map1.Contains(0))
153     {
154         int32 RemoveValNum = Map1.Remove(0);
155         UE_LOG(LogTemp, Display, TEXT("RemoveValNum: %d"), RemoveValNum);
156         ForItMap(Map1);
157     }
158 
159     // 删除指定 Key 的元素,如果元素存在则会返回被删除内容的深拷贝。但会出现没有找到对应 Key 以至于抛出异常,这时候去使用他的返回值会导致内存崩掉
160     FString FindAndRemoveString = Map1.FindAndRemoveChecked(1);
161     UE_LOG(LogTemp, Display, TEXT("FindAndRemoveString: %s"), *FindAndRemoveString);
162     ForItMap(Map1);
163 
164     // 删除指定的 Key 的元素,如果元素存在则会将被删除元素的值赋值给传入的第二个参数,删除成功返回 True, 否则返回 False
165     FString RemoveAndCoryFString;
166     if (Map1.RemoveAndCopyValue(1, RemoveAndCoryFString))
167     {
168         UE_LOG(LogTemp, Display, TEXT("RemoveAndCoryFString: %s"), *RemoveAndCoryFString);
169         ForItMap(Map1);
170     }
171 
172     // 清空 Map
173     Map1.Empty();
174     ForItMap(Map1);
175 
176 }

 

posted @ 2023-05-14 22:56  索智源  阅读(457)  评论(0编辑  收藏  举报