How to use TMultiMap?

Hi, I want to use TMultiMap with FText or FStrings arrays, something like this:
TMultiMap<TArray<FText> keys, TArray<FText> values> map = { {"key", "key2"}, {"val"} };

I would like to be able to search for a value based on some keys. Is this how it works? Also, how should I write such a map ? Any help is appreciated. Thankyou !

there are books about c++ maps data struct on the net

TMap is an associative container where the key must be unique. TMultiMap is a TMap variant where the uniqueness condition is absent.

For example, if you want to create a city resident register:

  • you can create TMap, where the key is the address and the value is a collection of people living in (because the address SHOULD be unique within the city)
  • you can create TMultiMap where the key will be a resident and the value will be the address where he lives (because you can have many people in the city with the same name and surname)

But a map where the key is an array… looks very strange.

2 Likes

Oh, I thought having a TArray in a TMap wasn’t even supported:

UPROPERTY()
  TMap<int32, TArray<AActor*>> ActorMap;

Error: Nested containers are not supported.

You can use a struct as a key and also as value.

That’s only true if it is a UPROPERTY. If it’s just a naked member variable, nested containers work fine. Though I agree with Emaer that an array as the key is particularly strange and someone non-sensical.

I would like to implement something like this
https://stackoverflow.com/a/46190938

They are not working with a map but with a struct.
A full example of a struct usable in blueprint as well (USTRUCT):

USTRUCT(BlueprintType)
struct FPerson {
    GENERATED_BODY()

    UPROPERTY(BlueprintReadWrite, EditAnywhere)
        FString Title;

    UPROPERTY(BlueprintReadWrite, EditAnywhere)
        TSet<FString> Aliases;

    UPROPERTY(BlueprintReadWrite, EditAnywhere)
        FGuid UniqueID;

    // Initialize
    FPerson () {
        Title = "";
        UniqueID = FGuid::NewGuid();
    }
};

After that you can simply compare a struct for values by reading its values or implementing a custom == operator.

FPerson Person = FPerson();
Person.Title = "Ben";

FPerson AnotherPerson = FPerson();
AnotherPerson.Title = "Dave";

// Comparing by name only:
if (Person.Title == AnotherPerson.Title) {

}

You can implement the == operator like this:

    // Operators
    bool operator==(const FPerson & Other) const {
        return (
            UniqueID == Other.UniqueID 
        );
    }

You can then have your array of Persons:

TArray<FPerson> Persons;

for (FPerson PersonX : Persons) {
//   PersonX.Title
// PersonX.Aliases
// ***
}

Thankyou very much! for your detailed answer