Topic: hash_map help!


Author: dsgoldman@hotmail.com
Date: 1998/08/12
Raw View

>   cout << (ctr == MyMap.end() ? "not found" : (*ctr).second)) << endl;

Is there any reason why one would prefer (*ctr).first -vs- ctr->first?

dan

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ 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: veldy@visi.com (Thomas T. Veldhouse)
Date: 1998/08/12
Raw View
On 6 Aug 1998 15:46:45 GMT, dsgoldman@hotmail.com wrote:

>
>struct eqstr
>{
>  bool operator()(const char* s1, const char* s2) const
>  {
>    return strcmp(s1, s2) == 0;
>  }
>};
>
I think you need to create eqstr using the binary_function<> template.

binary_function
template<class Arg1, class Arg2, class Result>
    struct binary_function {
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Result result_type;
    };
The template class serves as a base for classes that define a member
function of the form:

result_type operator()(first_argument_type, second_argument_type)

Hence, all such binary functions can refer to their first argument
type as first_argument_type, their second argument type as
second_argument_type, and their return type as result_type.


So ...

 class eqstr : public binary_function<char*, char*, bool>
 {
  bool operator()(char *s1, char *s2)
  {
   return strcmp(s1, s2) == 0;
  }
 }

Tom Veldhouse
veldy@visi.com



[ 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: veldy@visi.com (Thomas T. Veldhouse)
Date: 1998/08/12
Raw View
On 6 Aug 1998 15:46:45 GMT, dsgoldman@hotmail.com wrote:

>
>struct eqstr
>{
>  bool operator()(const char* s1, const char* s2) const
>  {
>    return strcmp(s1, s2) == 0;
>  }
>};
>
I think you need to create eqstr using the binary_function<> template.

binary_function
template<class Arg1, class Arg2, class Result>
    struct binary_function {
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Result result_type;
    };
The template class serves as a base for classes that define a member
function of the form:

result_type operator()(first_argument_type, second_argument_type)

Hence, all such binary functions can refer to their first argument
type as first_argument_type, their second argument type as
second_argument_type, and their return type as result_type.


So ...

 class eqstr : public binary_function<char*, char*, bool>
 {
  bool operator()(char *s1, char *s2)
  {
   return strcmp(s1, s2) == 0;
  }
 }

Tom Veldhouse
veldy@visi.com
---
[ 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/std-c++/faq.html                  ]





Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/08/12
Raw View
 <dsgoldman@hotmail.com> wrote:
>
>
>>   cout << (ctr == MyMap.end() ? "not found" : (*ctr).second)) << endl;
>
>Is there any reason why one would prefer (*ctr).first -vs- ctr->first?

Only that one's compiler is broken, so that its library is
defined without operator-> for its iterators.

This used to be quite common: instantiating a template instantiated
all the members, even those unused.  If any of unused member was
invalid (such as operator-> for an iterator over int) the compiler
reported an error.  Fortunately it is rare to encounter a compiler
that has this bug, but one still finds libraries written to work
under them.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/



[ 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: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/08/12
Raw View
Thomas T. Veldhouse<veldy@visi.com> wrote:
> dsgoldman@hotmail.com wrote:
>>
>>struct eqstr
>>{ bool operator()(const char* s1, const char* s2) const
>>    { return strcmp(s1, s2) == 0; } };
>>
>I think you need to create eqstr using the binary_function<> template.
> ...
>[so that] all such binary functions can refer to their first argument
>type as first_argument_type, their second argument type as
>second_argument_type, and their return type as result_type.
> ...
> class eqstr : public binary_function<char*, char*, bool>
> { bool operator()(char *s1, char *s2)
>    { return strcmp(s1, s2) == 0; } }

I think you mean std::binary_function<>.  It's not strictly necessary
to derive from std::binary_function<>, but it certainly is more
convenient than rewriting all those typedefs by hand.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/



[ 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: dsgoldman@hotmail.com
Date: 1998/08/06
Raw View
I'm having some problems getting the stl container hash_map to function
correctly.  I'm using RedHat Linux 5.1 with all the upgrades.  I've also
tried sgi's stl headers, but neither work the way i think they should. Here
are my examples.  Please tell me if I am doing something incorrectly.




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


1.

hash_map<const char *, const char *, hash<const char*>, eqstr> MyMap;

If I do the following when the map is empty:
hash_map<const char *, const char *, hash<const char*>, eqstr>::iterator ctr
=MyMap.find("test")

 and then

 if( ctr!=MyMap.end() ){
  ...
  // This code gets executed even when the hash_map is empty!!!!!
 }

I would think that ctr would be at the end if MyMap.size()==0, but it never
is.

If the hash_map is empty shouldn't it return MyMap.end()??


2.

hash_map<const char *, const char *, hash<const char*>, eqstr> MyMap;

additem(){
char *item = new char[20];
strcpy(item,"test");
MyMap[item]="testing";
}

void main(){
additem();
MyMap["blah"]="blah";
hash_map<const char *, const char *, hash<const char*>, eqstr>::iterator ctr;

ctr= MyMap.find("test");
cout << ctr <<endl;
ctr = MyMap.find("blah");
cout << ctr<<endl;
}

this produces:

NULL
blah


and I would think it would  produce:

testing
blah

Help!!

thanks
-dan

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ 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/08/06
Raw View
First: hash_map<> isn't part of the current C++ standard.  (Perhaps,
when the standard is revised, hash_map<> will be added.)  So this
might not be the best newsgroup for this discussion.  I'm crossposting
to comp.lang.c++, and redirecting followups to that group.

Second: if hash_map::find() returns something other than end() for an
empty map, that is certainly a bug and we have to fix it.  However,
I'm not able to reproduce that bug.  When I compile and run the
following program, the output I get is "OK".

  #include <hash_map.h>
  #include <iostream.h>

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

  int main()
  {
    hash_map<const char *, const char *, hash<const char*>, eqstr> M;
    hash_map<const char *, const char *, hash<const char*>, eqstr>::iterator i
     = M.find("x");

    if (i != M.end())
      cout << "bad" << endl;
    else
      cout << "OK" << endl;
  }

Third: the problem with the test of adding strings to a hash_map is
that all you're adding is pointers, so you have to ask what the
pointers are pointing to.  In this case they're pointing to a local
array in the function additem(), and when additem returns, the
pointers are left dangling.

dsgoldman@hotmail.com writes:

> I'm having some problems getting the stl container hash_map to function
> correctly.  I'm using RedHat Linux 5.1 with all the upgrades.  I've also
> tried sgi's stl headers, but neither work the way i think they should. Here
> are my examples.  Please tell me if I am doing something incorrectly.
>
>
>
>
> struct eqstr
> {
>   bool operator()(const char* s1, const char* s2) const
>   {
>     return strcmp(s1, s2) == 0;
>   }
> };
>
>
> 1.
>
> hash_map<const char *, const char *, hash<const char*>, eqstr> MyMap;
>
> If I do the following when the map is empty:
> hash_map<const char *, const char *, hash<const char*>, eqstr>::iterator ctr
> =MyMap.find("test")
>
>  and then
>
>  if( ctr!=MyMap.end() ){
>   ...
>   // This code gets executed even when the hash_map is empty!!!!!
>  }
>
> I would think that ctr would be at the end if MyMap.size()==0, but it never
> is.
>
> If the hash_map is empty shouldn't it return MyMap.end()??
>
>
> 2.
>
> hash_map<const char *, const char *, hash<const char*>, eqstr> MyMap;
>
> additem(){
> char *item = new char[20];
> strcpy(item,"test");
> MyMap[item]="testing";
> }
>
> void main(){
> additem();
> MyMap["blah"]="blah";
> hash_map<const char *, const char *, hash<const char*>, eqstr>::iterator ctr;
>
> ctr= MyMap.find("test");
> cout << ctr <<endl;
> ctr = MyMap.find("blah");
> cout << ctr<<endl;
> }
>
> this produces:
>
> NULL
> blah
>
>
> and I would think it would  produce:
>
> testing
> blah
>
> Help!!
>

[ 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: saroj@bear.com
Date: 1998/08/06
Raw View
In article <6qcaie$e5l$1@nnrp1.dejanews.com>,
  dsgoldman@hotmail.com wrote:


> 2.
>
> hash_map<const char *, const char *, hash<const char*>, eqstr> MyMap;
>
> additem(){
> char *item = new char[20];
> strcpy(item,"test");
> MyMap[item]="testing";
> }
>
> void main(){
> additem();
> MyMap["blah"]="blah";
> hash_map<const char *, const char *, hash<const char*>, eqstr>::iterator ctr;
>
> ctr= MyMap.find("test");
> cout << ctr <<endl;

Change this to
  cout << (ctr == MyMap.end() ? "not found" : (*ctr).second) << endl;
     (Note: (*ctr).first gives you the key, (*ctr).second the value )

> ctr = MyMap.find("blah");
> cout << ctr<<endl;

Change this to:
  cout << (ctr == MyMap.end() ? "not found" : (*ctr).second)) << endl;
>
> this produces:
>
> NULL
> blah
>
> and I would think it would  produce:
>
> testing
> blah
>

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ 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              ]