Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > [STL] [Maps] operator[]

Reply
Thread Tools

[STL] [Maps] operator[]

 
 
r.simoni
Guest
Posts: n/a
 
      10-22-2006
Hi, i have seen that this operator returns a reference to TYPE and not
a const reference.

I have a problem and I can't solve it:
i have a properties class with a get_property(const string &) function
that is const due to the fact that this method doesn't change the
"content" of properties class. If I write the method as this:
const string& get_property(const string& key) const {
return all_prop[key];
}

i receive an error in compile. The only way I have found to solve this
is to define class member all_prop as mutable. Is it correct? Is there
another way for coding this?
Thanks to all
Bye

--
Roberto Simoni

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      10-22-2006
r.simoni wrote:

>
> i receive an error in compile. The only way I have found to solve this
> is to define class member all_prop as mutable. Is it correct? Is there
> another way for coding this?
> Thanks to all


There is no map operator[] that is const. The problem is the
definition of the operator modifies the map if there isn't
already a pair in the map that matches.

It's clunky but you can do:

const string& get_property(const string& key) const {
map::<string, string>::conost_iterator it = all_props.find(key);
if(it != all_props.end())
return (*it).second;
else
return string();
}
 
Reply With Quote
 
 
 
 
Clark S. Cox III
Guest
Posts: n/a
 
      10-22-2006
r.simoni wrote:
> Hi, i have seen that this operator returns a reference to TYPE and not
> a const reference.
>
> I have a problem and I can't solve it:
> i have a properties class with a get_property(const string &) function
> that is const due to the fact that this method doesn't change the
> "content" of properties class. If I write the method as this:
> const string& get_property(const string& key) const {
> return all_prop[key];
> }
>
> i receive an error in compile. The only way I have found to solve this
> is to define class member all_prop as mutable. Is it correct?


Your problem is that std::map doesn't have a const operator[]. The way
that operator[] is defined by the standard can modify the map itself.

> Is there
> another way for coding this?


You need to use the find() function:

const string& get_property(const string& key) const
{
std::map<string,string>::const_iterator i = all_prop.find(key);
return (i == all_prop.end())?string():i->second;
}

--
Clark S. Cox III
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
LR
Guest
Posts: n/a
 
      10-22-2006
r.simoni wrote:

> const string& get_property(const string& key) const {
> return all_prop[key];
> }
>
> i receive an error in compile. The only way I have found to solve this
> is to define class member all_prop as mutable. Is it correct? Is there
> another way for coding this?



// asumming that...
typedef std::map<std::string,std::string> AllPropertiesMap;
// and
AllPropertiesMap all_prop;
// then
const std::string &get_property(const std::string &key) const {
AllPropertiesMap::const_iterator i = all_prop.find(key);
if(i != all_prop.end()) return i->second;
//
// here you have some options.
// you could return a const static local
// you could throw
// maybe something else
}

Generally, I think that I would prefer:

static const std::string &bad_property() {
static const std::string bp = "Bad Property";
return bp;
}

const std::string &get_property(const std::string &key) const {
AllPropertiesMap::const_iterator i = all_prop.find(key);
return i != all_prop.end() ? i->second : bad_property();
}

But undoubtedly, YMWV according to the requirements of your application.

HTH

LR
 
Reply With Quote
 
LR
Guest
Posts: n/a
 
      10-22-2006
Ron Natalie wrote:


> const string& get_property(const string& key) const {
> map::<string, string>::conost_iterator it = all_props.find(key);
> if(it != all_props.end())
> return (*it).second;
> else
> return string();

^^^^^^^^
Can you please tell me what the life time of the temp object in the
above return statement is?

TIA

> }


LR
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      10-22-2006
LR wrote:
> Ron Natalie wrote:
>
>
>> const string& get_property(const string& key) const {
>> map::<string, string>::conost_iterator it = all_props.find(key);
>> if(it != all_props.end())
>> return (*it).second;
>> else
>> return string();

> ^^^^^^^^
> Can you please tell me what the life time of the temp object in the
> above return statement is?
>
>

Oops, you're right. I forgot the thing was a returning
a reference. Bad thing. I originally just had a comment
there since it wasn't clear what the original poster wanted
to do in that case.

 
Reply With Quote
 
r.simoni
Guest
Posts: n/a
 
      10-23-2006

Ron Natalie ha scritto:

> LR wrote:
> > Ron Natalie wrote:
> >
> >
> >> const string& get_property(const string& key) const {
> >> map::<string, string>::conost_iterator it = all_props.find(key);
> >> if(it != all_props.end())
> >> return (*it).second;
> >> else
> >> return string();

> > ^^^^^^^^
> > Can you please tell me what the life time of the temp object in the
> > above return statement is?
> >
> >

> Oops, you're right. I forgot the thing was a returning
> a reference. Bad thing. I originally just had a comment
> there since it wasn't clear what the original poster wanted
> to do in that case.


Thanks to all.
The problem is that in my book, I don't have a well organized section
for STL. So i don't remember that to use a map in "constant mode" i
have to use a const_iterator
Thanks

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments