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

X

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, int> hm1;
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.
 
B

Barry

xz said:
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, int> hm1;
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
 
X

xz

operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, int> hm1;
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_map> or <hash_map.h> ?

I thought it should be
#include<hash_map>

but turns out:

error: hash_map: No such file or directory
 
X

xz

operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, int> hm1;
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,
 
X

xz

operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, int> hm1;
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'
 
X

xz

operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, int> hm1;
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'
 
X

xz

operator[] Inserts an element into a hash_map with a specified
key value.
And such example is given:
hash_map <int, int> hm1;
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'
 
B

Barry

xz said:
xz said:
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, int> hm1;
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_map> or <hash_map.h> ?

I thought it should be
#include<hash_map>

but turns out:

error: hash_map: No such file or directory

hash_map is not in standard, still extension(now is in tr1, unordered_map)

#include <hash_map> or <hash_map.h> is implementation defined (as it's
not standard)

moreover, hash_map is in namespace std or not is implementation defined
 
A

Alf P. Steinbach

* xz:
And should I include <hash_map> or <hash_map.h> ?

I thought it should be
#include<hash_map>

but turns out:

error: hash_map: No such file or directory

It's not a standard header.

So do whatever your /documentation/ tells you.

In the old days we just said RTFM (Read The Fucking Manual), or RTFMM,
but the younger generation, brought up on movies like Kill Bill I and
II, is so sensitive about "anti-American" terminology (nobody faqs in
America) that we can't even redirect them to <url: ftp://rtfm.mit.edu/>
to read the faqs, without a high chance of mortally offending them. And
as a consequence of that political correctness, as I recall Wikipedia
doesn't even provide the correct and only meaning of RTFM, but some
made-up US double-morality-compatible children's version. Oh well, but
anyway: RTFM, in your case <url: http://msdn.microsoft.com/library/>.

Cheers,

- Alf (slightly bad mood)
 
B

Barry

xz said:
xz said:
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, int> hm1;
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
 
X

xz

xz said:
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, int> hm1;
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.

And should I include <hash_map> or <hash_map.h> ?
I thought it should be
#include<hash_map>
but turns out:
error: hash_map: No such file or directory

hash_map is not in standard, still extension(now is in tr1, unordered_map)

#include <hash_map> or <hash_map.h> is implementation defined (as it's
not standard)

moreover, hash_map is in namespace std or not is implementation defined

The weird thing is that if I
#include<hash_map.h>

I got

In file included from /usr/lib/gcc/x86_64-linux-gnu/4.1.2/../../../../
include/c++/4.1.2/backward/hash_map.h:59,
from Bond.h:1,
from Bond.cpp:3:
/usr/lib/gcc/x86_64-linux-gnu/4.1.2/../../../../include/c++/4.1.2/
backward/backward_warning.h:32:2: warning: #warning This file includes
at least one deprecated or antiquated header. Please consider using
one of the 32 headers found in section 17.4.1.2 of the C++ standard.
Examples include substituting the <X> header for the <X.h> header for C
++ includes, or <iostream> instead of the deprecated header
<iostream.h>. To disable this warning use -Wno-deprecated.


However, if I
#include<hash_map>

I got

error: hash_map: No such file or directory



What should I do to use hash map?
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......
 
T

tony_in_da_uk

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_map> or <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
 
X

xz

xz said:
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, int> hm1;
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.
 
J

James Kanze

This is simple? This is hideous compared to the natural operator[]
usage.

Still, the STL version is worse:

std::map< X, int >::const_iterator it = hm1.find( key ) ;
int a = it == hm1.end() ? 42 : *hm1 ;

The real problem in reading a map (or a hashmap) is how to
handle missing values. Sometimes, an exception is appropriate,
but it's not a good general solution. So you need something out
of band.

Whatever the documentation for your system tells you. The
latest draft says <unordered_set>, but this is fairly recent,
and it's likely that your implementation still uses something
pre-standard. Which it *should* document.
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

Given that the correct name is unordered_set, searching for a
file whose name includes *hash* isn't going to help. More
generally, of course, recursive directory search is almost
certainly the wrong way to go about it here.
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....

The advantage of std::map is that it has been in the standard
from the very beginning, and so is standard. The various
pre-standard implementations of hash_map varied somewhat, which
created no end of portability problems. In the near future, of
course, there's std::unordered_map. But I don't think you can
count on it yet.

Note too that std::map guarantees O(n ln n). unordered_set will
be *typically* O(n), but only if you have a good hashing
function on the key. And good hashing functions are not
necessarily trivial to come by.
 
J

James Kanze

xz wrote:
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

They're not equivalent, or at least, I don't think so. The
operator[] in C++ map and unordered_map inserts, and can't be
used on a const object or through a const reference. In
practice, it's close to useless.

Java's HashMap is also more complicated than you indicate, since
it cannot contain int's, but only Integer's.

Roughly speaking, to read an element in C++, you need to:

Map::iterator elem = hm.find( key ) ;
if ( elem == hm.end() ) {
// Doesn't exist...
} else {
T value = elem->second ;
// ...
}

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

BobR

/* """ quote

Whatever the documentation for your system tells you. The
latest draft says <unordered_set>, but this is fairly recent,
and it's likely that your implementation still uses something
pre-standard. Which it *should* document.
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

Given that the correct name is unordered_set, searching for a
file whose name includes *hash* isn't going to help. More
generally, of course, recursive directory search is almost
certainly the wrong way to go about it here.
""" */

On an older implementation[1], it can help sometimes. I have no
'unordered_set', but, I do have:
....\Dev-Cpp\include\c++\3.3.1\backward\hash_map.h

.... so, if I wanted(?) to use it, I'd do:

#include <backward/hash_map.h>
// TODO: NOTE: OLD header.
// Update to <unordered_set> on next upgrade.


[1] - like that one poor guy whos boss would not upgrade from vc6.
 
X

xz

Thank you guys for all the suggestions and critisisms.

I am not the kind of persons who are reluctant to read "fucking
manual".
I never asked such stupid questions when coding in Java, since there
is the awesome "Java API speicification" available there.
I think my problem is I am not familiar with the manuals of C++.
Actually I posted the orignial post kinda because I got confused when
looking at the MSDN.

I did not know that hash_map is not in stadard until you guys told me
so.

When I searched "hash map" in msdn, it listed:

hash_map> (C++ Std Lib)
Defines the container template classes hash_map and hash_multimap and
their supporting templates. In Visual C++ .NET 2003, members of the
<hash_map> and <hash_set> header files are no longer in the ...
http://msdn2.microsoft.com/en-us/library/6x7w9f6z(vs.71).aspx

hash_map Class (Standard C++ Library)
Stores and retrieves data quickly from a collection in which each
element is a pair that has a sort key whose value is unique and an
associated data value.
http://msdn2.microsoft.com/en-us/library/0d462wfh(VS.80).aspx

I thought this second entry is in the "Standard C++ Library", which is
"standard".

The confusion about which to include, <hash_map> or <hash_map.h>, is
also from MSDN. The manual in MSDN told me "Header: <hash_map>" and
also gave an example code which #include <hash_map>.
(see http://msdn2.microsoft.com/en-us/library/8zz3703d(VS.80).aspx)

Now I resolved the problem by using map as suggested by Tony. Thanks!
 
X

xz

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

xz

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, couble> and 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
 
J

James Kanze

Now I simply use map <double, double> and it works.

That's what I'd do too:). std::map requires an ordering
relationship, and there's a very good one already defined for
double. A hash map requires a hash function, and I've yet to
figure out a good way to write one for double. (In general.
For specific cases, where the doubles are e.g. known to be in a
limited range, it should be possible.)
I only need the "map" or "hash map" for a small table with 5
or 6 key- value pairs.

In which case, std::map is probably even faster than a hash
table. For that matter, up to about 10 or 20 elements, linear
search in a vector or a C style table can be the fastest
solution. I actually use this a lot for constant tables, with C
style arrays, so that I can use static initialization, and avoid
order of initialization problems.
So my code is really not demanding for efficiency.
By "good", if you mean "efficient", then I have nothing to tell
you.

By "good", I mean a hash function that, given two arbitrary
values that compare different, the probability is very high that
the hash value will be different. And of course, given two
values that compare the same, the hash value is guaranteed to be
the same.
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top