hash_map

J

Jon Cosby

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.
 
P

P.J. Plauger

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
 
G

Gianni Mariani

Jon said:
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;

}
 
M

Mike Wahler

Jon Cosby said:
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
 
M

Mike Wahler

Mike Wahler said:
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
 
J

Jon Cosby

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
 
M

Mike Wahler

Jon Cosby said:
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
 
D

David Fisher

Jon Cosby said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,124
Latest member
JuniorPell
Top