Sign in to follow this  

c++ map where keys are strings

This topic is 3813 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
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
Quote:
Original post by Antheus
char * is exactly the same as std::string.

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


Argh I think I lost a few brain cells reading that. They are actually completely different, in that a char* is a normal pointer and std:string is a class. The only thing they have in common is that they're used to represent strings, and std:string problably uses a char* under the hood.

Plus the cost of lookup isn't the same. It's already been pointed out that the map will use the pointers address for key comparisions and is not a normal string comparision. Using char*'s as keys is a good way to completely break an application.

Share this post


Link to post
Share on other sites
Once again, I meant this :
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

This topic is 3813 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this