Topic: Viewing the contents of a const map&
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/03/18 Raw View
James Kuyper wrote:
>
> Sean Shubin wrote:
> >
> > Since the map operator [] can be used for both reading and writing
> > values to a map, what should I use to access a given value of a map
> > that is const?
> >
> > #include <iostream>
> > #include <string>
> > #include <map>
> >
> > using std::map;
> > using std::string;
> >
> > void
> > test(const map<string,string>& x)
> > {
> > using std::cout;
> > cout << x["hello"] << endl;
>
> cout << x.find("hello") << endl;
I think
cout << x.find("hello")->second << endl;
will me more like what he wants.
[...]
[ 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: James Kuyper <kuyper@wizard.net>
Date: 1999/03/19 Raw View
Christopher Eltschka wrote:
>
> James Kuyper wrote:
> >
> > Sean Shubin wrote:
> > >
> > > Since the map operator [] can be used for both reading and writing
> > > values to a map, what should I use to access a given value of a map
> > > that is const?
> > >
> > > #include <iostream>
> > > #include <string>
> > > #include <map>
> > >
> > > using std::map;
> > > using std::string;
> > >
> > > void
> > > test(const map<string,string>& x)
> > > {
> > > using std::cout;
> > > cout << x["hello"] << endl;
> >
> > cout << x.find("hello") << endl;
>
> I think
>
> cout << x.find("hello")->second << endl;
>
> will me more like what he wants.
It certainly will! Sorry.
[ 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: 1999/03/20 Raw View
Joe Buck wrote:
>
> sean.shubin@www.anything.com (Sean Shubin) writes:
> >Since the map operator [] can be used for both reading and writing
> >values to a map, what should I use to access a given value of a map
> >that is const?
>
> The reason you can't use [] with a const map is because there isn't
> anything that can be done if the key is not present in the map
> (well, OK, you could throw an exception).
If the const operator[] returned by value, it could also return
a default constructed value. This is what the non-const operator[]
does as well, except that it puts that into the map as well.
If you use only the operator[] interface, you even won't see
the difference at all. If you are using the iterator interface,
you'll see the difference, of course - but since it's logical
that a const operator[] won't change the map, I don't see a
problem with this.
[...]
---
[ 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: sean.shubin@www.anything.com (Sean Shubin)
Date: 1999/03/17 Raw View
Since the map operator [] can be used for both reading and writing
values to a map, what should I use to access a given value of a map
that is const?
#include <iostream>
#include <string>
#include <map>
using std::map;
using std::string;
void
test(const map<string,string>& x)
{
using std::cout;
cout << x["hello"] << endl;
//Does not compile because
//map operator [] is not const
}
int
main(int argc,char** argv)
{
map<string,string> x;
x["hello"] = "world";
test(x);
return 0;
}
[ 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: James Kuyper <kuyper@wizard.net>
Date: 1999/03/18 Raw View
Sean Shubin wrote:
>
> Since the map operator [] can be used for both reading and writing
> values to a map, what should I use to access a given value of a map
> that is const?
>
> #include <iostream>
> #include <string>
> #include <map>
>
> using std::map;
> using std::string;
>
> void
> test(const map<string,string>& x)
> {
> using std::cout;
> cout << x["hello"] << endl;
cout << x.find("hello") << endl;
Of course, this is appropriate only if you're certain that there is a
"hello" key in the map. If not, you'll have to add logic checking
whether or not x.find("hello")==x.end(). You haven't indicated how you
want that case handled, so I didn't insert the appropriate logic.
> //Does not compile because
> //map operator [] is not const
> }
>
> int
> main(int argc,char** argv)
> {
> map<string,string> x;
> x["hello"] = "world";
> test(x);
> return 0;
> }
[ 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: jbuck@synopsys.com (Joe Buck)
Date: 1999/03/18 Raw View
sean.shubin@www.anything.com (Sean Shubin) writes:
>Since the map operator [] can be used for both reading and writing
>values to a map, what should I use to access a given value of a map
>that is const?
The reason you can't use [] with a const map is because there isn't
anything that can be done if the key is not present in the map
(well, OK, you could throw an exception).
>void
>test(const map<string,string>& x)
>{
> using std::cout;
> cout << x["hello"] << endl;
>//Does not compile because
>//map operator [] is not const
>}
The answer is to use the find method, which returns an iterator.
// assume appropriate using, etc.
void test(const map<string,string>& x)
{
map<string,string>::const_iterator p = x.find("hello");
// p either points to a pair<const Key,Value> or to "one past the end"
if (p == x.end())
cout << "No entry for x["hello"]\n";
else
cout << (*p).second << endl;
}
(If you have an up-to-date compiler you can write p->second).
--
The reasonable man adapts himself to the world; the unreasonable man
persists in trying to adapt the world to himself. Therefore, all progress
depends on the unreasonable man. -----George Bernard Shaw
---
[ 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 ]