Problem finding FString Key within TMap

Hi all,

I have a TMap which I am constructing using a FString Key and a Struct for the Value (this struct is just 2 FStrings) from a XML parsed file.

I am parsing the XML file correctly and populating the TMap with the data, but I am having problems finding the ‘key’ with the TMap after construction. Here is my code so far…

const FXmlFile file(L"D:/file.xml");
const FXmlNode* SceneNode = file.GetRootNode();
FString scenename = SceneNode->GetTag();

// Get all child nodes
TArray<FXmlNode*> nodes = SceneNode->GetChildrenNodes();
TMap<FString, FLanguageData*> LanguageLocalised;

// Create TMap
for (int i = 0; i < nodes.Num(); ++i)
	if (nodes[i]->GetTag() == "textid")
		FString english = nodes[i + 1]->GetContent();
		FString french = nodes[i + 2]->GetContent();

		// Create our Language Struct and add it to the Map file
		FLanguageData* value = new FLanguageData();
		value->English = english;
		value->French = french;

		FString key = nodes[i]->GetContent();
		LanguageLocalised.Add(key, value);
// Contains
bool contains = LanguageLocalised.Contains("key1");
if (contains == true)
	UE_LOG(LogTemp, Warning, TEXT("TRUE"));
	UE_LOG(LogTemp, Warning, TEXT("FALSE"));

// Loop through Map
for (auto& KVP : LanguageLocalised)
	FString Key = KVP.Key;
	FLanguageData* ld = KVP.Value;

	UE_LOG(LogTemp, Warning, TEXT("Search for keys: %s"), *Key);
	UE_LOG(LogTemp, Warning, TEXT("Search for value: %s"), *ld->English);

My basic aim is to output the data from the corresponding key value, “key1”. You can see from my OutputLog (see image below) that the contains if state is returning FALSE. You can also see from this log that ‘key1’ is in the TMap (see Loop through Map in code snippet).

If I hardcode an additional entry into the TMap I can find it with a ‘contains’ just fine, so I’m guessing it’s related to my ‘FString key = nodes[i]->GetContent();’.

As I’m passing in a ‘hardcoded’ string into TMap.Contains(“key1”) or TMap.Contains(TEXT(“key1”)), and this is being compared against the original stored key string which came from a pointer: nodes[i]->GetContent(); (XmlParser), would this be why the comparison is failing?

My knowledge of c++ is one of learning, so any help would be greatly received.

Best regards,

I don’t know if you’re doing a localisation thing as practice for XML loading, but I should point out that UE4 has a localisation system built in.

Given your format string is "Search for keys: %s", and your output is Search for keys: "key1", that would suggest to me that the keys (and values) stored in your map are actually wrapped in quotes? (ie, "key1" rather than key1).

You are very correct!!

repeatedly slaps face with palm of hand! It’s funny how sometime you (meaning I) can look at something a million times and not see the obvious. Many thanks!!

Hey fallingbrickwork,

just a question. Do you delete your FLanguageData structs somewhere?
You are storing FLanguageData pointer in a TMap. If you don’t have a Function where you empty the Map and delete all these pointers you could encounter memory leak problems.
If you did everything right, ignore this comment :smiley:

Good Luck