469,902 Members | 1,804 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

hash_map

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.
Jul 22 '05 #1
10 4392
"Jon Cosby" <qw****@abc.net> wrote in message
news:ps******************@newsread1.news.pas.earth link.net...
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
Jul 22 '05 #2
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;

}

Jul 22 '05 #3

"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:c1*******************@nwrddc02.gnilink.net...
"Jon Cosby" <qw****@abc.net> wrote in message
news:ps******************@newsread1.news.pas.earth link.net...
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
Jul 22 '05 #4

"Jon Cosby" <qw****@abc.net> wrote in message
news:Qb*******************@newsread2.news.pas.eart hlink.net...

"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:c1*******************@nwrddc02.gnilink.net...
"Jon Cosby" <qw****@abc.net> wrote in message
news:ps******************@newsread1.news.pas.earth link.net...
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
Jul 22 '05 #5

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:JP******************@newsread2.news.pas.earth link.net...

"Jon Cosby" <qw****@abc.net> wrote in message
news:Qb*******************@newsread2.news.pas.eart hlink.net...

"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:c1*******************@nwrddc02.gnilink.net...
"Jon Cosby" <qw****@abc.net> wrote in message
news:ps******************@newsread1.news.pas.earth link.net...

> 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
Jul 22 '05 #6

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:JP******************@newsread2.news.pas.earth link.net...
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
Jul 22 '05 #7
Jon Cosby wrote:

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:JP******************@newsread2.news.pas.earth link.net...
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)
Jul 22 '05 #8

"Jon Cosby" <qw****@abc.net> wrote in message
news:Sh******************@newsread1.news.pas.earth link.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:JP******************@newsread2.news.pas.earth link.net...
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
Jul 22 '05 #9
"Jon Cosby" <qw****@abc.net> wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:JP******************@newsread2.news.pas.earth link.net...
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
Jul 22 '05 #10
> "Ideally, both sorted and hashed associated containers should be in the
C++
Standard Library ..."


Oops, I meant "associative containers" ...

David F
Jul 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Florian Liefers | last post: by
3 posts views Thread by Mark | last post: by
5 posts views Thread by peter_k | last post: by
1 post views Thread by jayesah | last post: by
2 posts views Thread by Amit Bhatia | last post: by
4 posts views Thread by James Kanze | last post: by
5 posts views Thread by frankw | last post: by
2 posts views Thread by marek.vondrak | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.