S
Simon Elliott
Here's a small example which uses std::maps within std::maps. Note the
line flagged // *** copy?
#include <iostream>
#include <map>
#include <string>
struct Tfoo
{
Tfoo():i1_(0),i2_(0){}
Tfoo(int i1, int i2):i1_(i1),i2_(i2){}
int i1_;
int i2_;
};
typedef std::map<std::string, Tfoo> TfooMapInner;
typedef std::map<std::string, TfooMapInner> TfooMapOuter;
int main(int argc, char **argv)
{
TfooMapOuter myFooMapOuter;
{
TfooMapInner myFooMapInner;
myFooMapInner["inner_key_1"] = Tfoo(0,1);
myFooMapInner["inner_key_2"] = Tfoo(0,2);
myFooMapOuter["outer_key_1"] = myFooMapInner;
}
{
TfooMapInner myFooMapInner;
myFooMapInner["inner_key_3"] = Tfoo(0,3);
myFooMapInner["inner_key_4"] = Tfoo(0,4);
myFooMapOuter["outer_key_2"] = myFooMapInner;
}
TfooMapOuter::const_iterator ipOuter;
TfooMapOuter::const_iterator epOuter = myFooMapOuter.end();
for (ipOuter=myFooMapOuter.begin(); ipOuter!=epOuter; ++ipOuter)
{
const std::string& outerKey = (*ipOuter).first;
const TfooMapInner& fooMapInner = (*ipOuter).second; // *** copy?
TfooMapInner::const_iterator ipInner;
TfooMapInner::const_iterator epInner = fooMapInner.end();
for (ipInner=fooMapInner.begin(); ipInner!=epInner; ++ipInner)
{
const std::string& innerKey = (*ipInner).first;
const Tfoo& foo = (*ipInner).second;
cout << outerKey << " " << innerKey << " " << foo.i1_ << " "
<< foo.i2_ << std::endl;
}
}
return 0;
}
This produces the output I expected:
outer_key_1 inner_key_1 0 1
outer_key_1 inner_key_2 0 2
outer_key_2 inner_key_3 0 3
outer_key_2 inner_key_4 0 4
But I'm concerned about the line I flagged (// *** copy?)
const TfooMapInner& fooMapInner = (*ipOuter).second;
Is (*ipOuter).second a copy of the TfooMapInner? If so, how can I avoid
this unnecessary copy being made?
line flagged // *** copy?
#include <iostream>
#include <map>
#include <string>
struct Tfoo
{
Tfoo():i1_(0),i2_(0){}
Tfoo(int i1, int i2):i1_(i1),i2_(i2){}
int i1_;
int i2_;
};
typedef std::map<std::string, Tfoo> TfooMapInner;
typedef std::map<std::string, TfooMapInner> TfooMapOuter;
int main(int argc, char **argv)
{
TfooMapOuter myFooMapOuter;
{
TfooMapInner myFooMapInner;
myFooMapInner["inner_key_1"] = Tfoo(0,1);
myFooMapInner["inner_key_2"] = Tfoo(0,2);
myFooMapOuter["outer_key_1"] = myFooMapInner;
}
{
TfooMapInner myFooMapInner;
myFooMapInner["inner_key_3"] = Tfoo(0,3);
myFooMapInner["inner_key_4"] = Tfoo(0,4);
myFooMapOuter["outer_key_2"] = myFooMapInner;
}
TfooMapOuter::const_iterator ipOuter;
TfooMapOuter::const_iterator epOuter = myFooMapOuter.end();
for (ipOuter=myFooMapOuter.begin(); ipOuter!=epOuter; ++ipOuter)
{
const std::string& outerKey = (*ipOuter).first;
const TfooMapInner& fooMapInner = (*ipOuter).second; // *** copy?
TfooMapInner::const_iterator ipInner;
TfooMapInner::const_iterator epInner = fooMapInner.end();
for (ipInner=fooMapInner.begin(); ipInner!=epInner; ++ipInner)
{
const std::string& innerKey = (*ipInner).first;
const Tfoo& foo = (*ipInner).second;
cout << outerKey << " " << innerKey << " " << foo.i1_ << " "
<< foo.i2_ << std::endl;
}
}
return 0;
}
This produces the output I expected:
outer_key_1 inner_key_1 0 1
outer_key_1 inner_key_2 0 2
outer_key_2 inner_key_3 0 3
outer_key_2 inner_key_4 0 4
But I'm concerned about the line I flagged (// *** copy?)
const TfooMapInner& fooMapInner = (*ipOuter).second;
Is (*ipOuter).second a copy of the TfooMapInner? If so, how can I avoid
this unnecessary copy being made?