f



Returning map from map of maps without any copy

Group,
I have a class that contains (STL) map of maps. This Class has a getMap 
function that return one map out of the map of maps. I need to avoid 
copies while extracting the map. There are two approaches
1. I pass a "map" reference in getMap function, the getMap method 
extract appropriate map and put it in the passed map reference. It seems 
copy will be made while doing
	map=Extracted Map; //map is reference parameter in getMap function

2. Second option is to return a reference from the getMap function 
(please ignore dangling pointer issue since this class is likely to live 
forever). My doubt is
	- Can I return a iterator->second as reference
	- If I can return it, how can caller save it (without making a copy).

Please note that maps to be returned are big and copying them is going 
to be a big overhead.'

Is there a better solution to handle this.
thanks
Ittium

0
ittium (65)
12/3/2011 7:08:03 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

4 Replies
904 Views

Similar Articles

[PageSpeed] 52

On 12/ 3/11 08:08 PM, ittium wrote:
> Group,
> I have a class that contains (STL) map of maps. This Class has a getMap
> function that return one map out of the map of maps. I need to avoid
> copies while extracting the map. There are two approaches
> 1. I pass a "map" reference in getMap function, the getMap method
> extract appropriate map and put it in the passed map reference. It seems
> copy will be made while doing
> 	map=Extracted Map; //map is reference parameter in getMap function
>
> 2. Second option is to return a reference from the getMap function
> (please ignore dangling pointer issue since this class is likely to live
> forever). My doubt is
> 	- Can I return a iterator->second as reference
> 	- If I can return it, how can caller save it (without making a copy).
>
> Please note that maps to be returned are big and copying them is going
> to be a big overhead.'
>
> Is there a better solution to handle this.

One way is to return a proxy object to a map.  Some like this:

#include <iostream>
#include <map>

typedef std::map<int,int>   Inner;
typedef std::map<int,Inner> Outer;

struct Container
{
   Outer map;

   struct OuterProxy
   {
     const int index;
     Outer&    mine;

     OuterProxy( Outer& outer, int n ) : mine(outer), index(n) {}

     int operator[]( int n ) { return mine[index][n]; }
   };

   OuterProxy operator[]( int n ) { return OuterProxy( map, n ); }
};

int main( int argc, char** argv )
{
   Container container;

   Inner inner;

   inner[42] = 21;

   container.map[2] = inner;

   std::cout << container[2][42] << std::endl;

   return 0;
}



-- 
Ian Collins
0
ian-news (10155)
12/3/2011 7:36:50 AM
On Sat, 2011-12-03, ittium wrote:
> Group,
> I have a class that contains (STL) map of maps. This Class has a getMap 
> function that return one map out of the map of maps. I need to avoid 
> copies while extracting the map. There are two approaches
> 1. I pass a "map" reference in getMap function, the getMap method 
> extract appropriate map and put it in the passed map reference. It seems 
> copy will be made while doing
> 	map=Extracted Map; //map is reference parameter in getMap function
>
> 2. Second option is to return a reference from the getMap function 
> (please ignore dangling pointer issue since this class is likely to live 
> forever). My doubt is
> 	- Can I return a iterator->second as reference
> 	- If I can return it, how can caller save it (without making a copy).
>
> Please note that maps to be returned are big and copying them is going 
> to be a big overhead.'
>
> Is there a better solution to handle this.

I don't really understand. It seems irrelevant that the inner type is
also a map, and that you've wrapped the outer map in a class. So
you're basically saying:

  I have a std::map<Key, Foo>
  where Foo is really large and I don't want to copy it. I want to
  "extract" a Foo without copying it.

You can easily get a Foo& from the map (using e.g. operator[] if you
know the key exists) -- std::map clearly supports this. So where is
the problem?

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .
0
nntp24 (1801)
12/3/2011 9:27:56 AM
On Dec 3, 8:08=A0am, ittium <itt...@gmail.com> wrote:
> Group,
> I have a class that contains (STL) map of maps. This Class has a getMap
> function that return one map out of the map of maps. I need to avoid
> copies while extracting the map.

Your question is full of irrelevant details. Your class in "I have a
class" doesn't matter. STL doesn't matter. Map of maps doesn't matter.

There's one thing that's actually relevant, and that is: how to return
an object from a function, if the price to copy the object is
prohibitive.

When you ask like that, the answer is easy: you can't. The only way to
avoid a copy is to return a reference. That's it.

If your function needs to find something, and you deem absence of that
something a normal occurrence, you might want to return a pointer.

> There are two approaches
> 1. I pass a "map" reference in getMap function, the getMap method
> extract appropriate map and put it in the passed map reference. It seems
> copy will be made while doing
> =A0 =A0 =A0 =A0 map=3DExtracted Map; //map is reference parameter in getM=
ap function
>
> 2. Second option is to return a reference from the getMap function
> (please ignore dangling pointer issue since this class is likely to live
> forever). My doubt is
> =A0 =A0 =A0 =A0 - Can I return a iterator->second as reference

Yes, provided that you don't erase the actual map entry later.

> =A0 =A0 =A0 =A0 - If I can return it, how can caller save it (without mak=
ing a copy).

Only by keeping the reference. That's it. If you need to copy that
around, you'll need a pointer (p =3D &thing_returned_from_function).

Goran.
0
goran.pusic (304)
12/3/2011 10:05:57 AM
On 12/3/2011 12:38 PM, ittium wrote:
> Group,
> I have a class that contains (STL) map of maps. This Class has a getMap
> function that return one map out of the map of maps. I need to avoid
> copies while extracting the map. There are two approaches
> 1. I pass a "map" reference in getMap function, the getMap method
> extract appropriate map and put it in the passed map reference. It seems
> copy will be made while doing
> map=Extracted Map; //map is reference parameter in getMap function
>
> 2. Second option is to return a reference from the getMap function
> (please ignore dangling pointer issue since this class is likely to live
> forever). My doubt is
> - Can I return a iterator->second as reference
> - If I can return it, how can caller save it (without making a copy).
>
> Please note that maps to be returned are big and copying them is going
> to be a big overhead.'
>
> Is there a better solution to handle this.
> thanks
> Ittium
>

Thanks everyone for the explanation. I will return the appropriate map 
by reference and save it in reference. This will avoid all the copies 
and solve the issue.
0
ittium (65)
12/8/2011 5:59:31 AM
Reply: