By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,463 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,463 IT Pros & Developers. It's quick & easy.

hash_map

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
"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

P: n/a
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: n/a

"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

P: n/a

"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

P: n/a

"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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
"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

P: n/a
> "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.