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

I cannot use the hash_map::operator[] to read the value in the hash map?

P: n/a
xz
>From the reference of MSDN about hash map:

operator[] Inserts an element into a hash_map with a specified
key value.

And such example is given:

hash_map <int, inthm1;
hm1[ 2 ] = 40;

However, there is no example like the following:

int a = hm1[2];

Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?

What I am looking for is just like the get() method in HashMap of Java.

Aug 2 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
xz wrote:
>>From the reference of MSDN about hash map:

operator[] Inserts an element into a hash_map with a specified
key value.

And such example is given:

hash_map <int, inthm1;
hm1[ 2 ] = 40;
like km1.put(2, 40); in Java
>
However, there is no example like the following:

int a = hm1[2];
like int a = (int) hm1.get(2); in Java
>
Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?

What I am looking for is just like the get() method in HashMap of Java.
Aug 2 '07 #2

P: n/a
xz
On Aug 1, 10:58 pm, Barry <dh...@126.comwrote:
xz wrote:
>From the reference of MSDN about hash map:
operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, inthm1;
hm1[ 2 ] = 40;

like km1.put(2, 40); in Java
However, there is no example like the following:
int a = hm1[2];

like int a = (int) hm1.get(2); in Java
Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?
What I am looking for is just like the get() method in HashMap of Java.
Thanks.

And should I include <hash_mapor <hash_map.h?

I thought it should be
#include<hash_map>

but turns out:

error: hash_map: No such file or directory
Aug 2 '07 #3

P: n/a
xz
On Aug 1, 10:58 pm, Barry <dh...@126.comwrote:
xz wrote:
>From the reference of MSDN about hash map:
operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, inthm1;
hm1[ 2 ] = 40;

like km1.put(2, 40); in Java
However, there is no example like the following:
int a = hm1[2];

like int a = (int) hm1.get(2); in Java
Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?
What I am looking for is just like the get() method in HashMap of Java.
error: 'class __gnu_cxx::hash_map<double, double,
__gnu_cxx::hash<double>, std::equal_to<double>, std::allocator<double>
>' has no member named 'put'
Aug 2 '07 #4

P: n/a
xz
On Aug 1, 10:58 pm, Barry <dh...@126.comwrote:
xz wrote:
>From the reference of MSDN about hash map:
operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, inthm1;
hm1[ 2 ] = 40;

like km1.put(2, 40); in Java
However, there is no example like the following:
int a = hm1[2];

like int a = (int) hm1.get(2); in Java
Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?
What I am looking for is just like the get() method in HashMap of Java.
error: 'class __gnu_cxx::hash_map<double, double,
__gnu_cxx::hash<double>, std::equal_to<double>,
std::allocator<double' has no member named 'put'

Aug 2 '07 #5

P: n/a
xz
On Aug 1, 10:58 pm, Barry <dh...@126.comwrote:
xz wrote:
>From the reference of MSDN about hash map:
operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, inthm1;
hm1[ 2 ] = 40;

like km1.put(2, 40); in Java
However, there is no example like the following:
int a = hm1[2];

like int a = (int) hm1.get(2); in Java
Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?
What I am looking for is just like the get() method in HashMap of Java.
error: 'class __gnu_cxx::hash_map<double, double,
__gnu_cxx::hash<double>, std::equal_to<double>,
std::allocator<double' has no member named 'put'

Aug 2 '07 #6

P: n/a
xz
On Aug 1, 10:58 pm, Barry <dh...@126.comwrote:
xz wrote:
>From the reference of MSDN about hash map:
operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, inthm1;
hm1[ 2 ] = 40;

like km1.put(2, 40); in Java
However, there is no example like the following:
int a = hm1[2];

like int a = (int) hm1.get(2); in Java
Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?
What I am looking for is just like the get() method in HashMap of Java.
error: 'class __gnu_cxx::hash_map<double, double,
__gnu_cxx::hash<double>, std::equal_to<double>,
std::allocator<double' has no member named 'put'

Aug 2 '07 #7

P: n/a
xz wrote:
On Aug 1, 10:58 pm, Barry <dh...@126.comwrote:
>xz wrote:
>>From the reference of MSDN about hash map:
operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, inthm1;
hm1[ 2 ] = 40;
like km1.put(2, 40); in Java
>>However, there is no example like the following:
int a = hm1[2];
like int a = (int) hm1.get(2); in Java
>>Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?
What I am looking for is just like the get() method in HashMap of Java.

error: 'class __gnu_cxx::hash_map<double, double,
__gnu_cxx::hash<double>, std::equal_to<double>,
std::allocator<double' has no member named 'put'
maybe you got me wrong,
I said what you said in your original post is right

C++ Java
hm[1] = 2 hm.put(1, 2)
int i = hm[1] int i = (int) hm.get(1);

I just said they are equivalent, not meant that C++ hash_map has put/get
member functions
Aug 2 '07 #8

P: n/a
On Aug 2, 1:47 pm, xz <zhang.xi...@gmail.comwrote:
like km1.put(2, 40); in Java
like int a = (int) hm1.get(2); in Java
I think hash map is so basic data structure, why is it such a headache
to use this famous HASH MAP?
In java, life is simple......
This is simple? This is hideous compared to the natural operator[]
usage.
And should I include <hash_mapor <hash_map.h?

I thought it should be
#include<hash_map>

but turns out:

error: hash_map: No such file or directory
If you want to be a programmer, you should at least know how to do a
recursive directory search for filenames including *hash*. Really,
kids these days.... ;-P

FWIW, many people just use std::map, which has a similar interface,
the benefit of sorting the elements, is standardised and portable
(just #include <map>), and is unlikely to have a noticeable
performance difference unless you're inserting tens of thousands or
more elements for frequent lookup or the elements are difficult to
compare. Basically, map does a binary lookup which typically requires
approximately log2(N) element comparisons, or 10 for a thousand
elements, 20 for a million....

Tony

Aug 2 '07 #9

P: n/a
xz
On Aug 2, 12:06 am, Barry <dh...@126.comwrote:
xz wrote:
On Aug 1, 10:58 pm, Barry <dh...@126.comwrote:
xz wrote:
From the reference of MSDN about hash map:
operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, inthm1;
hm1[ 2 ] = 40;
like km1.put(2, 40); in Java
>However, there is no example like the following:
int a = hm1[2];
like int a = (int) hm1.get(2); in Java
>Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?
What I am looking for is just like the get() method in HashMap of Java.
error: 'class __gnu_cxx::hash_map<double, double,
__gnu_cxx::hash<double>, std::equal_to<double>,
std::allocator<double' has no member named 'put'

maybe you got me wrong,
I said what you said in your original post is right

C++ Java
hm[1] = 2 hm.put(1, 2)
int i = hm[1] int i = (int) hm.get(1);

I just said they are equivalent, not meant that C++ hash_map has put/get
member functions
Now I got it. Thanks.

BTW, you may be using the HashMap in Jave incorrectly here.
HashMap in Java cannot deal with int, it can only deal with classes,
like Integer.
Aug 2 '07 #10

P: n/a
xz
On Aug 2, 6:52 am, James Kanze <james.ka...@gmail.comwrote:
>
Note that the original poster had declared a hash table of
double. I'd like to see the hash code function for that; I've
not found a good way to hash floating point to date.
Aug 2 '07 #11

P: n/a
xz
On Aug 2, 6:52 am, James Kanze <james.ka...@gmail.comwrote:
Note that the original poster had declared a hash table of
double. I'd like to see the hash code function for that; I've
not found a good way to hash floating point to date.
Now I simply use map <double, coubleand it works.

I only need the "map" or "hash map" for a small table with 5 or 6 key-
value pairs. So my code is really not demanding for efficiency.
By "good", if you mean "efficient", then I have nothing to tell
you.ace Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 2 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.