aspose file tools*
The moose likes C / C++ and the fly likes boost::unordered map equality function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "boost::unordered map equality function" Watch "boost::unordered map equality function" New topic
Author

boost::unordered map equality function

Rodrigo Bossini
Ranch Hand

Joined: Jul 03, 2009
Posts: 108
Hi

I've been using a boost::unordered_map <const char *,std::vector <const char *> > to store data coming from a file.

My problem is that the comparison between the keys of the map is not working as I'd expect..

For example, in the file I have a key whose name is "block_size" that is stored correctly in the map and its associated vector is stored correctly in the map with no problems.

But when I run a get ("block_size") in my map, it says it could not find that key in the map, although right before running the get function I had checked the entire map and made sure that the key was there along with its
associated vector.

I've made some research and found out that it could be needed to inform the map which comparison function it should use to compare the keys. I then started using a



boost::unordered_map <const char *,std::vector ><const char *>, boost::hash<const char *>,eqstr >

And my eqstr is like this:


But now something weird is happening..I run the same exact code many times..and each time I get a different result..sometimes it finds the key in the map..sometimes it says the key is not there.. the same exact code..
with no changes..shows different results each time I run it...how could that be?

I even tried using std::equal_to<const char *> to compare the keys, but then my map can't ever find the key although I'm sure it is there... has anyone seen this before?


I see wind mills
Anthony Aj Williams
author
Ranch Hand

Joined: Jun 10, 2011
Posts: 56
Rodrigo Bossini wrote:
I've been using a boost::unordered_map <const char *,std::vector <const char *> > to store data coming from a file.
For example, in the file I have a key whose name is "block_size" that is stored correctly in the map and its associated vector is stored correctly in the map with no problems.

But when I run a get ("block_size") in my map, it says it could not find that key in the map, although right before running the get function I had checked the entire map and made sure that the key was there along with its
associated vector.


I am not surprised that this is not working. The keys in this map are pointers. The hash algorithm and comparison therefore depend on the value of the pointer, not the pointed-to string.

Use std::string instead: boost::unordered_map <std::string,std::vector <const char*> >;

It's probably also worth checking the lifetime of the pointers used in the associated vector too, and maybe substituting std::string for those as well.


Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++11 thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

While I agree that the OP is better off using std::string than const char *, I am surprised that the standard does not require specialization for the family of \0 terminated array of characters for their hash containers.

After all -

- does not output the pointer location.

To the OP -- unordered_map is part of the new standard, and you should find it in the std:: namespace in reasonably new compilers.


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Anthony Aj Williams
author
Ranch Hand

Joined: Jun 10, 2011
Posts: 56
Anand Hariharan wrote:I am surprised that the standard does not require specialization for the family of \0 terminated array of characters for their hash containers.


If they did, this would mean you couldn't use a char* with such containers if it just pointed to a plain char rather than a null-terminated string without providing your own hash function and comparison function.

char* strings are not something that should be recommended for new code, std::string or equivalent platform-specific string types such as QString or CString should be used instead in almost all cases. As such, make special-case allowances for char* strings seems wrong.

Also, if you have to provide a comparison function for your unordered container, you probably also need to provide a hash function too --- the unordered containers require that the hash of equal items is the same, and this is unlikely to be the case if you provide one but not the other.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: boost::unordered map equality function
 
Similar Threads
Dount in K&B exam question
Geeky post: ode to the 3 things that I respect most in my computing history
Hashtable vs Hashmap
java.lang.UnsatisfiedLinkError
Finding Nth Largest element of an array without sorting