Topic: Problem with hash_map Iterator


Author: johna@daldd.sc.ti.com (John Apostol)
Date: 1998/10/22
Raw View


I'm trying to simply dump out all values of a hash_map
container, but I'm getting an infinite loop in the
iteration below.

  typedef hash_map<const char*,int,hash<const char*>,eqstr> HASHM;
  typedef HASHM::const_iterator HASHMI;

  HASHM h;
  h.insert( make_pair("xyz", 10) );

  for(HASHMI i=h.begin();i!=h.end();i++) cout<<i->first<<" "<<i->second<<endl;

When I use a regular map , i.e. map<const char*, int> , then the iteration
works correctly.

Did I missunderstand hash_map iterator? or is there a problem with the
implementation perhaps?



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Jonathan H Lundquist" <fluxsmith@fluxsmith.com>
Date: 1998/10/23
Raw View
You don't say whose hash_map you're using.  Try the one at
http://www.mindspring.com/~fluxsmith/Programming/library.html.

If it fails on you, send me personal e-mail.

Unrelated to your problem, but a minor performance tip, change your i++ to
++i.

John Apostol wrote in message <70o0q6$qds$1@superb.csc.ti.com>...


>I'm trying to simply dump out all values of a hash_map
>container, but I'm getting an infinite loop in the
>iteration below.

>  typedef hash_map<const char*,int,hash<const char*>,eqstr> HASHM;
>  typedef HASHM::const_iterator HASHMI;

>  HASHM h;
>  h.insert( make_pair("xyz", 10) );
>
>  for(HASHMI i=h.begin();i!=h.end();i++) cout<<i->first<<"
"<<i->second<<endl;

>When I use a regular map , i.e. map<const char*, int> , then the iteration
>works correctly.

>Did I missunderstand hash_map iterator? or is there a problem with the
>implementation perhaps?

[ moderator's note: excessive quoting deleted. -sdc ]



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/10/23
Raw View
John Apostol wrote:

> I'm trying to simply dump out all values of a hash_map
> container, but I'm getting an infinite loop in the
> iteration below.

>   typedef hash_map<const char*,int,hash<const char*>,eqstr> HASHM;
>   typedef HASHM::const_iterator HASHMI;

>   HASHM h;
>   h.insert( make_pair("xyz", 10) );

>   for(HASHMI i=h.begin();i!=h.end();i++) cout<<i->first<<" "<<i->second<<endl;

> When I use a regular map , i.e. map<const char*, int> , then the iteration
> works correctly.

> Did I missunderstand hash_map iterator? or is there a problem with the
> implementation perhaps?

Since hash_map is no standard container, it's hard to say what's
correct.
However, it's reasonable to expect that hash_map complies with the
general container requirements, and then the loop should work.
Therefore I'd say it's a bug in the implementation.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: nimel@hem1.passagen.se
Date: 1998/10/23
Raw View
In article <70o0q6$qds$1@superb.csc.ti.com>,
  johna@daldd.sc.ti.com wrote:
>
> I'm trying to simply dump out all values of a hash_map
> container, but I'm getting an infinite loop in the
> iteration below.

There is no hash_map container in the standard.

/Niklas Mellin

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Matt Austern <austern@sgi.com>
Date: 1998/10/23
Raw View
johna@daldd.sc.ti.com (John Apostol) writes:

> I'm trying to simply dump out all values of a hash_map
> container, but I'm getting an infinite loop in the
> iteration below.
>
>   typedef hash_map<const char*,int,hash<const char*>,eqstr> HASHM;
>   typedef HASHM::const_iterator HASHMI;
>
>   HASHM h;
>   h.insert( make_pair("xyz", 10) );
>
>   for(HASHMI i=h.begin();i!=h.end();i++) cout<<i->first<<" "<<i->second<<endl;
>
> When I use a regular map , i.e. map<const char*, int> , then the iteration
> works correctly.
>
> Did I missunderstand hash_map iterator? or is there a problem with the
> implementation perhaps?

Most likely it's either a bug in the implementation, an incorrect
definition of the eqstr function object, or an incorrect memory
reference somewhere else in the program that just happens to be
trampling on memory that the hash_map is using.

The code above is correct, and I don't get any errors or infinite
loops when I compile and run it.  The following program works as
expected.

#include <hash_map.h>

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};

int main()
{
  typedef hash_map<const char*,int,hash<const char*>,eqstr> HASHM;
  typedef HASHM::const_iterator HASHMI;

  HASHM h;
  h.insert( make_pair("xyz", 10) );

  for(HASHMI i=h.begin();i!=h.end();i++) cout<<i->first<<" "<<i->second<<endl;
}



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]