Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using char* as a key in std::map

Tags:

c++

map

stdmap

I am trying to figure out why the following code is not working, and I am assuming it is an issue with using char* as the key type, however I am not sure how I can resolve it or why it is occuring. All of the other functions I use (in the HL2 SDK) use char* so using std::string is going to cause a lot of unnecessary complications.

std::map<char*, int> g_PlayerNames;  int PlayerManager::CreateFakePlayer() {     FakePlayer *player = new FakePlayer();     int index = g_FakePlayers.AddToTail(player);      bool foundName = false;      // Iterate through Player Names and find an Unused one     for(std::map<char*,int>::iterator it = g_PlayerNames.begin(); it != g_PlayerNames.end(); ++it)     {         if(it->second == NAME_AVAILABLE)         {             // We found an Available Name. Mark as Unavailable and move it to the end of the list             foundName = true;             g_FakePlayers.Element(index)->name = it->first;              g_PlayerNames.insert(std::pair<char*, int>(it->first, NAME_UNAVAILABLE));             g_PlayerNames.erase(it); // Remove name since we added it to the end of the list              break;         }     }      // If we can't find a usable name, just user 'player'     if(!foundName)     {         g_FakePlayers.Element(index)->name = "player";     }      g_FakePlayers.Element(index)->connectTime = time(NULL);     g_FakePlayers.Element(index)->score = 0;      return index; } 
like image 598
Josh Renwald Avatar asked Nov 11 '10 18:11

Josh Renwald


People also ask

Should I use std::string or * char?

Use std::string when you need to store a value. Use const char * when you want maximum flexibility, as almost everything can be easily converted to or from one.

How do I change the key-value of a map in C++?

To update an existing value in the map, first we will find the value with the given key using map::find() function. If the key exists, then will update it with new value.

Can a string be a key in map C++?

A map is an associative container that maps keys to values, provides logarithmic complexity for inserting and finding, and constant time for erasing single elements. It is common for developers to use a map to keep track of objects by using a string key.

What can be used as the key of map in C++?

We can have any of the primary data types or derived data types as key or value data types in a map. We can use any of the data types as the data type of the key of the map. Even a user-defined data type can be used as key data type.


2 Answers

You need to give a comparison functor to the map otherwise it's comparing the pointer, not the null-terminated string it points to. In general, this is the case anytime you want your map key to be a pointer.

For example:

struct cmp_str {    bool operator()(char const *a, char const *b) const    {       return std::strcmp(a, b) < 0;    } };  map<char *, int, cmp_str> BlahBlah; 
like image 51
GWW Avatar answered Sep 30 '22 11:09

GWW


You can't use char* unless you are absolutely 100% sure you are going to access the map with the exact same pointers, not strings.

Example:

char *s1; // pointing to a string "hello" stored memory location #12 char *s2; // pointing to a string "hello" stored memory location #20 

If you access map with s1 you will get a different location than accessing it with s2.

like image 31
Pablo Santa Cruz Avatar answered Sep 30 '22 12:09

Pablo Santa Cruz