Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::maps within std::maps -- optimisation

Reply
Thread Tools

std::maps within std::maps -- optimisation

 
 
Simon Elliott
Guest
Posts: n/a
 
      03-09-2005
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?


--
Simon Elliott http://www.ctsn.co.uk
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      03-09-2005
"Simon Elliott" <Simon at ctsn.co.uk> wrote:

> 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?


First insert the map, then fill it instead of the other way round:

TfooMapInner& myFooMapInner = myFooMapOuter["outer_key_2"];
myFooMapInner["inner_key_3"] = Tfoo(0,3);
myFooMapInner["inner_key_4"] = Tfoo(0,4);

 
Reply With Quote
 
 
 
 
simont
Guest
Posts: n/a
 
      03-09-2005
Dereferencing (*ipOuter).second gives you a reference to the inner map
instance which already exists in the outer map. Initialising a
reference from that (as you do) just gives it a name.

The only place a map is copied is when you're setting up (see Rolf's
suggestion for avoiding that). You aren't copying anything in the bit
you flagged.

 
Reply With Quote
 
Simon Elliott
Guest
Posts: n/a
 
      03-10-2005
On 09/03/2005, Rolf Magnus wrote:
> > Is (*ipOuter).second a copy of the TfooMapInner? If so, how can I
> > avoid this unnecessary copy being made?

>
> First insert the map, then fill it instead of the other way round:
>
> TfooMapInner& myFooMapInner = myFooMapOuter["outer_key_2"];
> myFooMapInner["inner_key_3"] = Tfoo(0,3);
> myFooMapInner["inner_key_4"] = Tfoo(0,4);


Thanks - that makes sense.

--
Simon Elliott http://www.ctsn.co.uk
 
Reply With Quote
 
Simon Elliott
Guest
Posts: n/a
 
      03-10-2005
On 09/03/2005, simont wrote:

> Dereferencing (*ipOuter).second gives you a reference to the inner map
> instance which already exists in the outer map. Initialising a
> reference from that (as you do) just gives it a name.


I take your point about the reference, but I was concerned that a
std:air might be created on the fly by the iterator. But now I come
to think about it, this wouldn't be much use if you wanted to use the
iterator to change the map data!

> The only place a map is copied is when you're setting up (see Rolf's
> suggestion for avoiding that). You aren't copying anything in the bit
> you flagged.


That's useful to know.

--
Simon Elliott http://www.ctsn.co.uk
 
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
Search Engine Optimisation sorry.no.email@post_NG.com HTML 0 05-08-2006 11:54 AM
Network speed (question on optimisation) Dima Computer Support 1 04-09-2004 09:05 PM
Optimisation of regexps in Perl? Fredrik Ramsberg Perl 2 10-15-2003 08:30 AM
boolean loop optimisation Roedy Green Java 8 09-12-2003 10:24 AM
optimisation for the web chaz Digital Photography 2 08-12-2003 09:35 PM



Advertisments