473,511 Members | 16,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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

Oct 22 '06 #1
6 1711
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();
}
Oct 22 '06 #2
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
cl*******@gmail.com
Oct 22 '06 #3
LR
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
Oct 22 '06 #4
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
Oct 22 '06 #5
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.

Oct 22 '06 #6

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

Oct 23 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
12680
by: Michael H Lees | last post by:
Hi there, I'm trying to use an stl map where I have my own defined (templated) class as a key. I have the class "variableId" below which I want to use as the key for an stl map. It has two...
4
2709
by: Tom | last post by:
Hi, I need some help with stl maps. I am using a std::map that is sorted in a very special way. Therefore I am using my own sort function. That works fine. But this is only half the problem....
4
19943
by: Daniel Heiserer | last post by:
Hello, I used a unique associative container such as: //--------------- map<vector<int>,double> X; //--------------- In general I fill X with millions of entries. Sorting plays no role...
6
8745
by: cylin | last post by:
Dear all, If I declared a map<int,string> MapA; How can I sort MapA by its data,i.e. string. for example: map<int,string> MapA; MapA=string("ccc"); MapA=string("bbb"); MapA=string("ddd");
4
2443
by: M?rio Amado Alves | last post by:
Will you help an outsider trying to trace the current state of persistent object technology? "I expect that there will be persistent object stores with STL-conforming interfaces fitting into the...
0
2050
by: Mona | last post by:
Newbie question - I am a newbie to c++ and stl maps. My question is based onSystemC which is a specialized C++ hardware programming language. I am trying to create a map with key of type sc_lv<22>...
5
1767
by: DrLex | last post by:
This is a really annoying thing to look up in Google because all pages that mention STL maps or vectors will most likely also contain the word "template". So maybe this question has been asked...
18
2928
by: Hunk | last post by:
Would like some advice on the fillowing I have a sorted list of items on which i require to search and retrieve the said item and also modify an item based on its identity. I think an Map stl...
3
2212
by: mahaveer | last post by:
Hi , I am facing a strange problem with STL maps ,I am using gdb to debug my debug my code. Upto one point the number of entries in the map is shown as follows , $79 = {_M_t = { _M_impl...
19
3494
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.