std::map design : use a std::string for the map key and the stored object name (member)

I tried to find the best way to design a named objects storage : std::map< std::string, NamedObject >

I would like to use a std::map where key is the name of the object. And in the other hand, I would like my object to be identifiable by itself, like using a getName() method and returning that name. The thing which irritates me is I didn't found a better way other than set two copies of the std::string, one for the key and one inside the object as member.

If anyone have a suggestion ?

EDIT:

Basically, this is a resources manager. The most important thing is to find a resource by his name. But sometime, when I have the resource alone under the hand, I would like to be able to know his name. In the first place, I thought about a vector... But the idea to loop trought it and ask every object his name to find the right one was bad to me.

Upvotes: 0

Views: 136

Answers (1)

Elena NNN
Elena NNN

Reputation: 217

Are you sure that you have such tough requirements for the memory? If yes, I think you can create any "Hash" function for the object name like this:

int hash( const std::string& obj_name );

and then use

std::multimap<int, NamedObject>

but it seems like extra complexity

Upvotes: 1

Related Questions