x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Change key in the TMap

How can I change the key value in TMap, C++? I am trying to get all the keys using TMap::GetKeys, in the parameter I set a pointer to the key type inside TMap, but when changed, only the pointer changes.

Product Version: Not Selected
Tags:
more ▼

asked Sep 10 '18 at 08:44 AM in C++ Programming

avatar image

NODARYN
122 10 21 33

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

This worked for me (using FStrings as keys):

 bool RenameItem(FString OldKey, FString NewKey)
 {
     if (!Items.Contains(OldKey) || Items.Contains(NewKey))
         return false;
 
     auto v = Items[OldKey]]; // Store the current value
     Items.Remove(OldKey);    // Get rid of the old key (and it's value)
     Items.Shrink();
     Items.Add(NewKey, v);    // Re-add the stored value using the new key

     return true;
 }
more ▼

answered Mar 02 '19 at 02:16 AM

avatar image

MichaelWion
250 25 40 50

avatar image CiborgCG Mar 03 '19 at 10:35 AM

What if we need to retain the order of the key/value pairs in the map? The only way I can think of is to loop through each pair and change the key directly, but not really sure if it is a good approach.

 for (auto& pair : yourTMap)
 {
     if (pair.Key == keyToReplace)
     {
         pair.Key = FString("new key value");
         break;
     }
 }

EDIT: Although my approach would change the key, this would be useless since the new key is not hashed and it wouldn't be possible to search the map with that key. Is there a way to re-hash the table after changing the key value?

avatar image madturtleGGinin Mar 04 '19 at 04:22 PM

In your code once you re-hash it the order will still change.

The nature of a hashmap is that elements are un-ordered. If your relies on its order then you're doing something wrong.

If you absolutely need an "ordered map", one way to implement it is using a linked list in conjunction with hashmap, and you traverse the linked list when you need ordered data.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question