• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

boost::unordered map equality function

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
author
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anthony Aj Williams
author
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic