does map find guarantee to not copy mapped value?

Discussion in 'C++' started by PeteUK, Sep 2, 2010.

  1. PeteUK

    PeteUK Guest

    Hello,

    I find myself writing a lot of code like the one below where I find
    something out of a map and then use a reference to second to
    interrogate the mapped object:

    #include <iostream>
    #include <map>

    class Expensive { /* lots of stuff here... */ };

    int main()
    {
    std::map<int,Expensive> m;
    m.insert(std::make_pair(1,Expensive(3)));

    // and then later
    std::map<int,Expensive>::const_iterator it = m.find(1);
    const Expensive& expensive = (*it).second;
    // use expensive here - is this safe?
    }

    Is this practice safe? I've just determined on VC9 compiler that the
    find doesn't do any copies, but wondered if the standard mandates
    this?

    Thanks,

    Pete
    PeteUK, Sep 2, 2010
    #1
    1. Advertising

  2. PeteUK

    Kai-Uwe Bux Guest

    PeteUK wrote:

    > Hello,
    >
    > I find myself writing a lot of code like the one below where I find
    > something out of a map and then use a reference to second to
    > interrogate the mapped object:
    >
    > #include <iostream>
    > #include <map>
    >
    > class Expensive { /* lots of stuff here... */ };
    >
    > int main()
    > {
    > std::map<int,Expensive> m;
    > m.insert(std::make_pair(1,Expensive(3)));
    >
    > // and then later
    > std::map<int,Expensive>::const_iterator it = m.find(1);
    > const Expensive& expensive = (*it).second;
    > // use expensive here - is this safe?
    > }
    >
    > Is this practice safe?


    Operations on map don't invalidate references unless they erase the element
    referred to. The same guarantee applies to iterators and pointers into the
    map.

    > I've just determined on VC9 compiler that the
    > find doesn't do any copies, but wondered if the standard mandates
    > this?


    Yes: [23.1/11] and [23.1.2/8] and the absence of invalidate-clauses in the
    section about std::map.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Sep 2, 2010
    #2
    1. Advertising

  3. PeteUK

    PeteUK Guest

    On 2 Sep, 11:13, Kai-Uwe Bux <> wrote:
    > PeteUK wrote:
    > > Hello,

    >
    > > I find myself writing a lot of code like the one below where I find
    > > something out of a map and then use a reference to second to
    > > interrogate the mapped object:

    >
    > > #include <iostream>
    > > #include <map>

    >
    > > class Expensive { /* lots of stuff here... */ };

    >
    > > int main()
    > > {
    > > std::map<int,Expensive> m;
    > > m.insert(std::make_pair(1,Expensive(3)));

    >
    > >         // and then later
    > > std::map<int,Expensive>::const_iterator it = m.find(1);
    > > const Expensive& expensive = (*it).second;
    > > // use expensive here - is this safe?
    > > }

    >
    > > Is this practice safe?

    >
    > Operations on map don't invalidate references unless they erase the element
    > referred to. The same guarantee applies to iterators and pointers into the
    > map.
    >
    > > I've just determined on VC9 compiler that the
    > > find doesn't do any copies, but wondered if the standard mandates
    > > this?

    >
    > Yes: [23.1/11] and [23.1.2/8] and the absence of invalidate-clauses in the
    > section about std::map.
    >
    > Best
    >
    > Kai-Uwe Bux


    Kai-Uwe Bux,

    I appreciate your clarification of this.

    Pete
    PeteUK, Sep 2, 2010
    #3
  4. PeteUK

    Marc Guest

    On 2 sep, 12:03, PeteUK <> wrote:
    >         std::map<int,Expensive> m;
    >         m.insert(std::make_pair(1,Expensive(3)));


    As a side note, you may want to avoid this syntax, as it may cause
    more copies than needed. Replacing std::make_pair with
    std::map<...>::value_type saves one copy in libstdc++ (or 2 in C++0x
    mode, because make_pair becomes horrible). std::make_pair(1,3) works
    too.

    (Yes, I know that most of those copies are actually moves in C++0x)
    Marc, Sep 2, 2010
    #4
    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. Jonas
    Replies:
    21
    Views:
    1,080
    Mark McIntyre
    Oct 14, 2003
  2. lovecreatesbeauty
    Replies:
    6
    Views:
    426
    SM Ryan
    Sep 23, 2005
  3. Pallav singh
    Replies:
    1
    Views:
    342
    Triple-DES
    Mar 27, 2008
  4. Roedy Green

    What does volatile guarantee?

    Roedy Green, Feb 12, 2010, in forum: Java
    Replies:
    96
    Views:
    2,235
    Roedy Green
    Feb 18, 2010
  5. Alf P. Steinbach /Usenet

    Does [stdint.h] guarantee two's complement form?

    Alf P. Steinbach /Usenet, Sep 20, 2010, in forum: C++
    Replies:
    2
    Views:
    585
    Alf P. Steinbach /Usenet
    Sep 20, 2010
Loading...

Share This Page