Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

stdext::hash_map example needed

This topic is 2805 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

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
Advertisement

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
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
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
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
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!