TPair< KeyType, ElementType > 엘리먼트를 가진 해시 컨테이너.

 

//TMap 생성 및 엘리먼트 추가.
TMap<int32, FString> FruitMap;
FruitMap.Add(5, TEXT("Banana"));
FruitMap.Add(2, TEXT("Grapefruit"));
FruitMap.Add(7, TEXT("Pineapple"));
// FruitMap == [
//  { Key: 5, Value: "Banana"     },
//  { Key: 2, Value: "Grapefruit" },
//  { Key: 7, Value: "Pineapple"  } ]

// TMultiMap이 아니라서 중복 허용 안함.
FruitMap.Add(2, TEXT("Pear"));
// FruitMap == [
//  { Key: 5, Value: "Banana"    },
//  { Key: 2, Value: "Pear"      },
//  { Key: 7, Value: "Pineapple" } ]

FruitMap.Emplace(3, TEXT("Orange"));
// FruitMap == [
//  { Key: 5, Value: "Banana"    },
//  { Key: 2, Value: "Pear"      },
//  { Key: 7, Value: "Pineapple" },
//  { Key: 3, Value: "Orange"    } ]

// Append를 이용한 삽입 병합.
TMap<int32, FString> FruitMap2;
FruitMap2.Emplace(4, TEXT("Kiwi"));
FruitMap2.Emplace(9, TEXT("Melon"));
FruitMap2.Emplace(5, TEXT("Mango"));
FruitMap.Append(FruitMap2);
// FruitMap == [
//  { Key: 5, Value: "Mango"     },
//  { Key: 2, Value: "Pear"      },
//  { Key: 7, Value: "Pineapple" },
//  { Key: 3, Value: "Orange"    },
//  { Key: 4, Value: "Kiwi"      },
//  { Key: 9, Value: "Melon"     } ]
// 이제 FruitMap2 은 비었음.

// 반복 처리.
for (auto& Elem : FruitMap)
{
    FPlatformMisc::LocalPrint(
        *FString::Printf(
            TEXT("(%d, \"%s\")\n"),
            Elem.Key,    // 5,2,7,3,4,9
            *Elem.Value // "Mango","Pear","Pineapple","Orange","Kiwi","Melon"
        )
    );
}

for (auto It = FruitMap.CreateConstIterator(); It; ++It)
{
    FPlatformMisc::LocalPrint(
        *FString::Printf(
            TEXT("(%d, \"%s\")\n"),
            It.Key(),   // same as It->Key
            *It.Value() // same as *It->Value
        )
    );
}

// 엘리먼트 개수.
int32 Count = FruitMap.Num(); // Count == 6

// 맵에 엘리먼트 있는지 확인.
bool bHas7 = FruitMap.Contains(7); // bHas7 == true
bool bHas8 = FruitMap.Contains(8); // bHas8 == false

// Find(key)로 value 탐색.
FString* Ptr7 = FruitMap.Find(7); // *Ptr7 == "Pineapple"
FString* Ptr8 = FruitMap.Find(8); //  Ptr8 == nullptr

// FindKey(value)로 key 탐색. 포인터 반환.
const int32* KeyMangoPtr   = FruitMap.FindKey(TEXT("Mango"));   // *KeyMangoPtr  == 5
const int32* KeyKumquatPtr = FruitMap.FindKey(TEXT("Kumquat")); // KeyKumquatPtr == nullptr

// 제거.
FruitMap.Remove(8);
// FruitMap == [
//  { Key: 5, Value: "Mango"     },
//  { Key: 2, Value: "Pear"      },
//  { Key: 7, Value: "Pineapple" },
//  { Key: 3, Value: "Orange"    },
//  { Key: 4, Value: "Kiwi"      },
//  { Key: 9, Value: "Melon"     } ]

// FindAndRemoveChecked 맵에서 엘리먼트 제거하고 그 값(value)를 반환.
FString Removed7 = FruitMap.FindAndRemoveChecked(7);
// Removed7 == "Pineapple"
// FruitMap == [
//  { Key: 5, Value: "Mango"  },
//  { Key: 2, Value: "Pear"   },
//  { Key: 4, Value: "Kiwi"   },
//  { Key: 3, Value: "Orange" },
//  { Key: 9, Value: "Melon"  } ]

// 맵 엘리먼트 모두 제거.
FruitMap.Empty(); // Reset()과 동일.
// FruitMap == []

// KeySort / ValueSort 소팅.
FruitMap.KeySort([](int32 A, int32 B) {
    return A > B; // sort keys in reverse
});
// FruitMap == [
//  { Key: 9, Value: "Melon"  },
//  { Key: 5, Value: "Mango"  },
//  { Key: 4, Value: "Kiwi"   },
//  { Key: 3, Value: "Orange" } ]

FruitMap.ValueSort([](const FString& A, const FString& B) {
    return A.Len() < B.Len(); // sort strings by length
});
// FruitMap == [
//  { Key: 4, Value: "Kiwi"   },
//  { Key: 5, Value: "Mango"  },
//  { Key: 9, Value: "Melon"  },
//  { Key: 3, Value: "Orange" } ]​

'::protected > 언리얼4&5' 카테고리의 다른 글

오브젝트(Object) 생성  (0) 2019.11.12
TSet  (0) 2019.10.17
TArray  (0) 2019.10.17
스트링 처리  (0) 2019.10.17
델리게이트  (0) 2019.10.17

+ Recent posts