Sign in to follow this  
lucky6969b

stdext::hash_map example needed

Recommended Posts

lucky6969b    1330
I'd like to use the x,y coordinates and time (Cooperative pathfinding) as the key for the "hashmap" in my project. How do I define a composite key like that on stdext::hash_map? Could you please raise an example? or a link/code snippets would be nice too...

Could anyone explain why this code doesn't crash,
http://www.codeguru.com/forum/showthread.php?t=315286
while mine does,


typedef stdext::hash_map<int, bool> pf_type;
pf_type pf;

cAStar::cAStar()
{
char buff[256];
pf[1] = false;
sprintf (buff, "pf[1] is %s\n", pf[1] ? "TRUE" : "FALSE");
OutputDebugString(buff);
}



The error was "Vector subscript out of range"


Thanks a lot :)
Jack

[Edited by - lucky6969b on November 10, 2010 4:09:37 AM]

Share this post


Link to post
Share on other sites
lucky6969b    1330

pf.insert(std::pair<int, bool>(1, false));
stdext::hash_map<int, bool>::iterator it;
it = pf.find(1);
sprintf (buff, "value is %s\n", it->second ? "TRUE" : "FALSE");
OutputDebugString(buff);



still of no use. There isn't any method in hash_map that allows me to reserve space for it. Still vector subscript out of range. Could anyone shed some lights on this?
Thanks
Jack

Share this post


Link to post
Share on other sites
rip-off    10976
Post a minimal compilable example of the program (just a main() function should suffice). What compiler are you using? Newer ones might have std::unordered_map available.

Is "it == pf.end()" in your latest example? Or where does the code crash?

Share this post


Link to post
Share on other sites
lucky6969b    1330
hi rip-off,

Thanks for reminding me, I put the code inside the constructor and thats why it crashed and now it works inside another spot.

But I have another question,
If I use this method in cooperative pathfinding, how can I define the key
Let's say
I have a structure [x][y][t]
Do I define a key like this, cos its a 3-dimensional array
((x * width)+y)*t_width)+t => key

Thanks
Jack

Share this post


Link to post
Share on other sites
rip-off    10976
You are going to be hashing them. That could work, but the distribution is important too. But you are supposed to define a hash_map<vec3, bool>, not a hash_map<int,bool>. The latter would gend up hashing twice.

Instead provide a hashing function which collapses the vec3 into an integral value. This is usually passed as an additional parameter to the hash_map template. I'm sure some Googling will illustrate this, I found this thread pretty quickly but I can't recommend any of the techniques as I've never tried them.

Share this post


Link to post
Share on other sites

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