P
PeteUK
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
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