Jump to content
  • Advertisement
Sign in to follow this  
bulgurmayo

c++ map where keys are strings

This topic is 4023 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In C++, what is the best way to create a map where keys are strings. Should I go for a std::map<std ::string, MyObject>, Or should I better use a std::map<const char*, MyObject> I know that char* are evil, but by using the first version I am afraid of costly string copies anytime the map is modified.

Share this post


Link to post
Share on other sites
Advertisement
use strings, its not that costly vs the pain in the ass you will run into having to create a new char * each time you add an entry, also you will have to remeber to delete map::iter->first to clean up those allocated char *'s

Share this post


Link to post
Share on other sites
Additionally to the pain std::map<const char*, MyObject> will not work as you think it would.

The map will use the actual pointer values for the sorting. It will also not be able to know that two char* strings containing the same text are the same if they are located at different addresses.

Share this post


Link to post
Share on other sites
This I knew, I was meant this :

class LessFctr {
public:
bool operator () ( const char* s1, const char* s2) const {return strcmp(s1, s2) < 0;}
};
std::map<const char*, MyObject, LessFctr>

Share this post


Link to post
Share on other sites
Quote:
Original post by bulgurmayo
Why aren't the string copies so costly ?
Isn't a whole copy going to take place everytime ?


Depends. If your library uses a Copy-On-Write optimization for its strings, then actual data copies only happen when you modify a string. Even if the strings are copies, short strings (less than 16 characters) are copied near-instantly anyway on most implementations, without even a memory allocation.

Besides, unless your program revolves entirely about manipulating the strings in this map for several minutes, I wouldn't worry about this, as the difference in performance will most likely be on the hundred-nanosecond-per-second scale.

Share this post


Link to post
Share on other sites
More importantly, copying around will only happen when the map contents are modified, i.e. when things are added (when they're removed, the map will simply reorganize "map nodes" in memory, which is a bunch of pointer manipulation, but no changes to the keys or copying of data). In normal use, you spend much, much more time looking things up in a map than you do on creating it, so the creation efficiency doesn't matter much.

Share this post


Link to post
Share on other sites
char * is exactly the same as std::string.

Except, like in soap operas, char * is the evil twin of the good doctor, the std::string.

The cost of lookup would be exactly the same in both cases, namely full string vs. string comparison.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!