[STL] [Maps] operator[]

R

r.simoni

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
 
R

Ron Natalie

r.simoni said:
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();
}
 
C

Clark S. Cox III

r.simoni said:
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;
}
 
L

LR

r.simoni said:
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
 
L

LR

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
 
R

Ron Natalie

LR said:
Ron Natalie wrote:


^^^^^^^^
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.
 
R

r.simoni

Ron Natalie ha scritto:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top