Returning map from map of maps without any copy

I

ittium

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
 
I

Ian Collins

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;
}
 
J

Jorgen Grahn

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
 
G

Goran

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

Yes, provided that you don't erase the actual map entry later.
        - If I can return it, how can caller save it (without making a copy).

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

Goran.
 
I

ittium

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top