[STL] [Maps] operator[]



 DEVELOP > c-Plus-Plus > [STL] [Maps] operator[]

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "r.simoni"
Date: 22 Oct 2006 01:23:01 PM
Object: [STL] [Maps] operator[]
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
.

User: "Ron Natalie"

Title: Re: [STL] [Maps] operator[] 22 Oct 2006 02:03:23 PM
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();
}
.
User: "LR"

Title: Re: [STL] [Maps] operator[] 22 Oct 2006 03:09:50 PM
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
.
User: "Ron Natalie"

Title: Re: [STL] [Maps] operator[] 22 Oct 2006 03:46:59 PM
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.
.
User: "r.simoni"

Title: Re: [Maps] operator[] 23 Oct 2006 03:07:01 AM
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
.




User: "Clark S. Cox III"

Title: Re: [STL] [Maps] operator[] 22 Oct 2006 02:15:20 PM
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
clarkcox3@gmail.com
.

User: "LR"

Title: Re: [STL] [Maps] operator[] 22 Oct 2006 02:29:43 PM
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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER