[STL] [Maps] operator[]

Discussion in 'C++' started by r.simoni, Oct 22, 2006.

  1. r.simoni

    r.simoni Guest

    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
     
    r.simoni, Oct 22, 2006
    #1
    1. Advertising

  2. r.simoni

    Ron Natalie Guest

    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();
    }
     
    Ron Natalie, Oct 22, 2006
    #2
    1. Advertising

  3. 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
     
    Clark S. Cox III, Oct 22, 2006
    #3
  4. r.simoni

    LR Guest

    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
     
    LR, Oct 22, 2006
    #4
  5. r.simoni

    LR Guest

    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
     
    LR, Oct 22, 2006
    #5
  6. r.simoni

    Ron Natalie Guest

    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.
     
    Ron Natalie, Oct 22, 2006
    #6
  7. r.simoni

    r.simoni Guest

    Re: [Maps] operator[]

    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
     
    r.simoni, Oct 23, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael H Lees

    STL maps and const keys

    Michael H Lees, Jun 25, 2003, in forum: C++
    Replies:
    3
    Views:
    5,133
    Thomas Matthews
    Jun 26, 2003
  2. Simon Elliott
    Replies:
    4
    Views:
    1,180
    Simon Elliott
    Mar 10, 2005
  3. Marcus
    Replies:
    2
    Views:
    611
    Marcus
    Dec 9, 2005
  4. Alden Pierre

    STL: Maps instance with the operator=

    Alden Pierre, May 3, 2006, in forum: C++
    Replies:
    2
    Views:
    357
    Alden Pierre
    May 3, 2006
  5. C++Liliput
    Replies:
    17
    Views:
    1,157
    Stefan Ram
    Oct 19, 2008
Loading...

Share This Page