Skip to main content
gamedev.net
🔒 Locked

Unordered Map Boost

Started by CyanCrey Mar 11, 2009 at 7:25 AM 4 replies 750+ views
Original Post
CyanCrey
CyanCrey
Hi, I have problems whith the BOOST container. GetMessageFunctorHashMap() returns an unordered_map. Then I try to insert a new item. The operation seems to be OK but when I request if the container is empty i allways recieve TRUE. Can anyone helps me, please? GetMessageFunctorHashMap()["Hello"]=(Functor*) new SpecificVoidFunctor(this, &MessageSystem::Exit); GetMessageFunctorHashMap().empty(); //returns 1 Thanks!
SiCrane
SiCrane
You probably want GetMessageFunctorHashMap() to return a reference not a value.
CyanCrey
CyanCrey
You were right! Thanks!

Now the code crashes in the string library when internally the Unordered_Map container tries to compare two strings. Anyone knows if unordered_map can compare and hash strings by default? Or i have to define the hash function and the compare function?

Thanks!
SiCrane
SiCrane
boost::unordered_map should work out of the box with std::string objects as keys.
loufoque
loufoque
Quote:
GetMessageFunctorHashMap()["Hello"]=(Functor*) new SpecificVoidFunctor(this, &MessageSystem::Exit);


1) That is obviously not exception-safe, but since your map is global, you probably don't care about leaks there.

2) Casts are pure evil, and C-style casts even more. The funny thing is that you DON'T need to cast, here. So why are you?

3) Why not use generic binding facilities instead of that hard-coded SpecificVoidFunctor thingy? Sure, generic binding returns functors of unspecified types, but you can use type erasure, which should be
a) fully compatible with any functor
b) more efficient
c) more optimal in terms of memory usage
d) exception-safe
CyanCrey
CyanCrey
Thanks everybody!

Finally I used a simple std::map, works OK and it's fine for the first version of my system. But I still don't known why the unordered_map and the hash_map containers don't works....Well....that remain a mystery xD.

loufoque:

1) Ok, I'll take in to account.
2) Yes, your right i don't know why i was using a cast there.
3) "generic binding facilities". Can you explain me more about that, please? I don't known what are you referring to.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.