[UE4] TMap用法

TMap<EGameKey::Type, FSimpleKeyState> KeyStateMap;

for (TMap<EGameKey::Type,FSimpleKeyState>::TIterator It(KeyStateMap); It; ++It)
{
	EGameKey::Type* const k = &It.Key();
	FSimpleKeyState* const v = &It.Value();
}

C++11风格:

TMap<FString, int32> MyMap;
// New style
for (auto& Kvp : MyMap)
{
	UE_LOG(LogCategory, Log, TEXT("Key: %s, Value: %d"), Kvp.Key, *Kvp.Value);
}

 注意事项:

1,如果for循环内执行remove操作,那么remove之后,当前的Element就不要在使用了,因为其指向的内存已被回收了,例如:

for (auto& Elem : CharacterList)
{
	...
	CharacterList.Remove(Elem.Key);
	//此时在使用Elem会出现寻址错误
	FVector Loc = Elem.Key->GetActorLocation();
	...
}

猜你喜欢

转载自aigo.iteye.com/blog/2273568