Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > does map find guarantee to not copy mapped value?

Reply
Thread Tools

does map find guarantee to not copy mapped value?

 
 
PeteUK
Guest
Posts: n/a
 
      09-02-2010
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
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-02-2010
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
 
Reply With Quote
 
 
 
 
PeteUK
Guest
Posts: n/a
 
      09-02-2010
On 2 Sep, 11:13, Kai-Uwe Bux <jkherci...@gmx.net> 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
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      09-02-2010
On 2 sep, 12:03, PeteUK <newbar...@gmail.com> 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: hashtable or map? (map inserts not behaving as I expect - and I cant find a decent simple example for hashtable) Kai-Uwe Bux C++ 1 12-21-2008 09:25 PM
Is there any module to map a network drive and remove a mapped network drive in perl king Perl Misc 1 10-22-2007 05:25 PM
Could not find part of path on mapped drive dhl365 ASP .Net 0 09-18-2007 05:46 PM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM
Could not find a part of the path when access a remote server via mapped drive or unc AL ASP .Net 0 10-14-2004 08:12 AM



Advertisments