Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Can't make a hash_map with std::string for key

Can't make a hash_map with std::string for key

Scheduled Pinned Locked Moved ATL / WTL / STL
questioncsharpc++cryptography
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jonathan Gilligan
    wrote on last edited by
    #1

    How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador?     James Merrill

    J Z S A J 6 Replies Last reply
    0
    • J Jonathan Gilligan

      How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador?     James Merrill

      J Offline
      J Offline
      John M Drescher
      wrote on last edited by
      #2

      I don't know how to do this but you need to write a hash function. John

      1 Reply Last reply
      0
      • J Jonathan Gilligan

        How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador?     James Merrill

        J Offline
        J Offline
        John M Drescher
        wrote on last edited by
        #3

        I found this on a web search:

        #include <iostream>
        #include <string>
        #include <hash_map>
        using namespace std;

        /** BEGIN FIX **/
        namespace std
        {
        template<> struct hash< std::string >
        {
        size_t operator()( const std::string& x ) const
        {
        return hash< const char* >()( x.c_str() );
        }
        };
        }
        /** END FIX **/

        hash_map <string, int> months;
        int main(void) {
        months["january"] = 31;
        months["february"] = 28;
        return 0;
        }

        John

        1 Reply Last reply
        0
        • J Jonathan Gilligan

          How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador?     James Merrill

          Z Offline
          Z Offline
          ZoogieZork
          wrote on last edited by
          #4

          Here's the hash function I've been using:

          namespace std {
          template<>
          size_t hash_compare<string>::operator()(const string &s) const
          {
          unsigned long retv = 0;
          for (string::const_iterator iter = s.begin();
          iter != s.end(); ++iter)
          {
          retv = 5 * retv + *iter;
          }
          return size_t(retv);
          }
          }

          There's probably a better way to do it, though. :) - Mike

          1 Reply Last reply
          0
          • J Jonathan Gilligan

            How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador?     James Merrill

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            The other ways, of course, to get working hash_maps would be to either

            1. use STLPort (where hash_map and hash_set have a hash comparison function defined for std::strings - and also you get nice features like debug mode, which tells you when you've made boo-boos like pushing an iterator outside a container, or used mismatched iterators)
            2. Upgrade to VS.NET 2003. It cost me GBP19 (USD30 roughly) and was worth every penny - especially for the way more compliant compiler and faster IDE (well, I think it is - subjectively).

            Of course, the solution in the other posts is rather more immediate :-D Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

            1 Reply Last reply
            0
            • J Jonathan Gilligan

              How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador?     James Merrill

              A Offline
              A Offline
              Alexandru Savescu
              wrote on last edited by
              #6

              Here's how Java get a hash code for the string. The algorithm should be fine: /** * Returns a hash code for this string. The hash code for a * String object is computed as *

               \* s\[0\]\*31^(n-1) + s\[1\]\*31^(n-2) + ... + s\[n-1\]
               \* 
              

              * using int arithmetic, where s[i] is the * _i_th character of the string, n is the length of * the string, and ^ indicates exponentiation. * (The hash value of the empty string is zero.) * * @return a hash code value for this object. */ Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

              1 Reply Last reply
              0
              • J Jonathan Gilligan

                How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador?     James Merrill

                J Offline
                J Offline
                Jonathan Gilligan
                wrote on last edited by
                #7

                Thanks to everyone who helped explain this. Thanks especially for the code fragments. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador?     James Merrill

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups