hash_map

Discussion in 'C++' started by Jon Cosby, Nov 30, 2003.

  1. Jon Cosby

    Jon Cosby Guest

    I need help in hashmaps. Why doesn't this work:

    #include <hash_map>

    hash_map <int, string> hm1;
    typedef pair <int, string> pr;
    hm1.insert(str_pair(1, "Hello"));

    It compiles, but crashes at runtime, pointing to something in xhash.

    --

    Jon Cosby

    Please do not reply to this email address.
     
    Jon Cosby, Nov 30, 2003
    #1
    1. Advertising

  2. Jon Cosby

    P.J. Plauger Guest

    "Jon Cosby" <> wrote in message
    news:pspyb.20341$...

    > I need help in hashmaps. Why doesn't this work:
    >
    > #include <hash_map>
    >
    > hash_map <int, string> hm1;
    > typedef pair <int, string> pr;
    > hm1.insert(str_pair(1, "Hello"));
    >
    > It compiles, but crashes at runtime, pointing to something in xhash.


    Depends a lot on what your undocumented str_pair does. It would
    be a really good idea for it to return something like:

    hash_map<int, string>::value_type(1, string("Hello"));

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
     
    P.J. Plauger, Nov 30, 2003
    #2
    1. Advertising

  3. Jon Cosby wrote:
    > I need help in hashmaps. Why doesn't this work:
    >
    > #include <hash_map>
    >
    > hash_map <int, string> hm1;
    > typedef pair <int, string> pr;
    > hm1.insert(str_pair(1, "Hello"));


    .... str_pair is not defined here.

    >
    > It compiles, but crashes at runtime, pointing to something in xhash.
    >


    hash_map is not part of the standard, however it is popular, I have no
    idea what the "right(TM)" way to get access to it under gcc so I'd check
    up on that. I used gcc 3.3.1.

    The code below works find for me. Maybe your str_pair() returns a
    reference local variable ?

    #include <ext/hash_map>
    #include <string>
    #include <iostream>

    using namespace std;
    using namespace __gnu_cxx;

    int main()
    {
    hash_map<int, string> hm1;

    typedef hash_map<int, string>::value_type pr;

    hm1.insert(pr(1, "Hello"));

    cout << hm1[1] << endl;

    }
     
    Gianni Mariani, Nov 30, 2003
    #3
  4. Jon Cosby

    Jon Cosby Guest

    "P.J. Plauger" <> wrote in message
    news:c1qyb.33542$...
    > "Jon Cosby" <> wrote in message
    > news:pspyb.20341$...
    >
    > > I need help in hashmaps. Why doesn't this work:
    > >
    > > #include <hash_map>
    > >
    > > hash_map <int, string> hm1;
    > > typedef pair <int, string> pr;
    > > hm1.insert(str_pair(1, "Hello"));
    > >
    > > It compiles, but crashes at runtime, pointing to something in xhash.

    >
    > Depends a lot on what your undocumented str_pair does.


    That was a typo. Should be just "pr".


    Jon
     
    Jon Cosby, Nov 30, 2003
    #4
  5. Jon Cosby

    Mike Wahler Guest

    "Jon Cosby" <> wrote in message
    news:Qbryb.22527$...
    >
    > "P.J. Plauger" <> wrote in message
    > news:c1qyb.33542$...
    > > "Jon Cosby" <> wrote in message
    > > news:pspyb.20341$...
    > >
    > > > I need help in hashmaps. Why doesn't this work:
    > > >
    > > > #include <hash_map>
    > > >
    > > > hash_map <int, string> hm1;
    > > > typedef pair <int, string> pr;
    > > > hm1.insert(str_pair(1, "Hello"));
    > > >
    > > > It compiles, but crashes at runtime, pointing to something in xhash.

    > >
    > > Depends a lot on what your undocumented str_pair does.

    >
    > That was a typo. Should be just "pr".


    Then P.J.'s reply is still essentially the same, but changing
    'str_pair' to 'pr'.

    BTW 'hash_map' is not part of standard C++ (although provided
    as an extension by many implementations).

    -Mike
     
    Mike Wahler, Nov 30, 2003
    #5
  6. Jon Cosby

    Mike Wahler Guest

    Re: [corr] hash_map

    "Mike Wahler" <> wrote in message
    news:JPtyb.22683$...
    >
    > "Jon Cosby" <> wrote in message
    > news:Qbryb.22527$...
    > >
    > > "P.J. Plauger" <> wrote in message
    > > news:c1qyb.33542$...
    > > > "Jon Cosby" <> wrote in message
    > > > news:pspyb.20341$...
    > > >
    > > > > I need help in hashmaps. Why doesn't this work:
    > > > >
    > > > > #include <hash_map>
    > > > >
    > > > > hash_map <int, string> hm1;
    > > > > typedef pair <int, string> pr;
    > > > > hm1.insert(str_pair(1, "Hello"));
    > > > >
    > > > > It compiles, but crashes at runtime, pointing to something in xhash.
    > > >
    > > > Depends a lot on what your undocumented str_pair does.

    > >
    > > That was a typo. Should be just "pr".

    >
    > Then P.J.'s reply is still essentially the same, but changing
    > 'str_pair' to 'pr'.


    Ack! Disregard that, I see you have defined the object 'pr'
    above. I didn't read carefully enough. Sorry!

    >
    > BTW 'hash_map' is not part of standard C++ (although provided
    > as an extension by many implementations).


    This does remain true.

    -Mike
     
    Mike Wahler, Nov 30, 2003
    #6
  7. Jon Cosby

    Jon Cosby Guest

    "Mike Wahler" <> wrote in message
    news:JPtyb.22683$...

    > BTW 'hash_map' is not part of standard C++ (although provided
    > as an extension by many implementations).
    >
    > -Mike


    Are you sure? VC lists it in the Standard C++ Library.

    Jon
     
    Jon Cosby, Nov 30, 2003
    #7
  8. Jon Cosby

    Pete Becker Guest

    Jon Cosby wrote:
    >
    > "Mike Wahler" <> wrote in message
    > news:JPtyb.22683$...
    >
    > > BTW 'hash_map' is not part of standard C++ (although provided
    > > as an extension by many implementations).
    > >
    > > -Mike

    >
    > Are you sure? VC lists it in the Standard C++ Library.
    >


    He's sure. And he's right.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Nov 30, 2003
    #8
  9. Jon Cosby

    Mike Wahler Guest

    "Jon Cosby" <> wrote in message
    news:Shuyb.20666$...
    >
    > "Mike Wahler" <> wrote in message
    > news:JPtyb.22683$...
    >
    > > BTW 'hash_map' is not part of standard C++ (although provided
    > > as an extension by many implementations).
    > >
    > > -Mike

    >
    > Are you sure?


    Yes.

    > VC lists it in the Standard C++ Library.


    VC does not define the C++ language.

    Also, I find no reference at all to 'hash_map' in
    my VC++6.0 documentation. Perhaps version 7 has
    such an animal, but it's still not standard.

    -Mike
     
    Mike Wahler, Dec 1, 2003
    #9
  10. Jon Cosby

    David Fisher Guest

    "Jon Cosby" <> wrote:

    > "Mike Wahler" <> wrote in message
    > news:JPtyb.22683$...
    >
    > > BTW 'hash_map' is not part of standard C++ (although provided
    > > as an extension by many implementations).
    > >
    > > -Mike

    >
    > Are you sure? VC lists it in the Standard C++ Library.


    The STL Tutorial and Reference Guide (2nd ed) says on page 161:

    "Ideally, both sorted and hashed associated containers should be in the C++
    Standard Library, but only sorted associated containers are included
    (unofficial STL-based hash table specifications and implementations are
    available)"

    BTW a reason given for this (on p.161) is that hash tables can be order (N)
    in the worst case, but that implies the use of linked lists at each hash
    node ... surely a binary tree, etc. could be used instead. Anyone know if
    std::maps and sets have been permitted to be implemented as hash tables
    since this book was written (2001) ?

    David F
     
    David Fisher, Dec 2, 2003
    #10
  11. Jon Cosby

    David Fisher Guest

    Re: hash_map (correction)

    > "Ideally, both sorted and hashed associated containers should be in the
    C++
    > Standard Library ..."


    Oops, I meant "associative containers" ...

    David F
     
    David Fisher, Dec 2, 2003
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kristofer Pettijohn

    2d hash_map iteration ?

    Kristofer Pettijohn, Jun 26, 2003, in forum: C++
    Replies:
    1
    Views:
    995
    Rob Williscroft
    Jun 26, 2003
  2. Jacek Generowicz

    Pre-standardizing hash_map & friends.

    Jacek Generowicz, Aug 26, 2003, in forum: C++
    Replies:
    0
    Views:
    361
    Jacek Generowicz
    Aug 26, 2003
  3. Charles Herman

    hash_map iterator

    Charles Herman, Nov 3, 2003, in forum: C++
    Replies:
    5
    Views:
    5,991
    Ron Natalie
    Nov 4, 2003
  4. Florian Liefers

    C2143, hash_map

    Florian Liefers, Nov 12, 2003, in forum: C++
    Replies:
    11
    Views:
    1,314
    Dan Cernat
    Nov 12, 2003
  5. Mark
    Replies:
    3
    Views:
    15,912
Loading...

Share This Page