Re: I cannot use the hash_map::operator[] to read the value in the hash map?
On Aug 2, 7:06 am, Barry <dh...@126.com> wrote:
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.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34