473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 4700
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Florian Liefers | last post by:
"Hello World\n", i get error C2143 (Syntaxerror, missing ';' before '<') using the following code: #include <hash_map> struct eqstr { bool operator()(const char* s1, const char* s2) const
3
by: Mark | last post by:
Hi, I'm trying to use hash_map (gcc 3.2.2) with a std::string as the key. It will compile if I use <map> but I get a bunch of template compile errors when I change it to hash_map. Any...
5
by: peter_k | last post by:
Hi I've defined hash_map in my code using this: ------------------------------------------- #include <string> #include <hash_map.h> & namespace __gnu_cxx {
3
by: kony | last post by:
Hi there, I would much appreciate your help with the following problem. Below is the code that uses a hash_map. I want to release all the memory occupied by the hash_map for other use. Apparently...
1
by: jayesah | last post by:
Hi All, I am developing my code with Apache stdcxx. I am bound to use STL of Apache only. Now today I need hash_map in code but as I learned, it is not available in Apache since it is not...
2
by: Amit Bhatia | last post by:
Hi, I am trying to use hash maps from STL on gcc 3.3 as follows: #ifndef NODE_H #define NODE_H #include <ext/hash_map> #include "node_hasher.h" class Node; typedef hash_map<pair<int,int>,...
4
by: James Kanze | last post by:
On Jul 16, 10:53 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote: It depends. You might like to have a look at my "Hashing.hh" header (in the code at kanze.james.neuf.fr/code-en.html---the...
5
by: frankw | last post by:
Hi, I have a hash_map with string as key and an object pointer as value. the object is like class{ public: float a; float b; ...
2
by: marek.vondrak | last post by:
Hi, I am wondering if there are any functional differences between SGI's hash_map and tr1's unordered_map. Can these two containers be interchanged? What would it take to switch from hash_map to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.