tags:

views:

89

answers:

2

I have a map like this:

map<prmNode,vector<prmEdge>,prmNodeComparator> nodo2archi;

I need to have an identical copy of this map. How is the fast way to make this? I have tried this:

map<prmNode,vector<prmEdge>,prmNodeComparator> copiamap( nodo2archi );

but it doesn't work. The copiamap is empty. Thank you very much

+3  A: 

Use the map's copy constructor:

map<prmNode,vector<prmEdge> > nodo2archi;
map<prmNode,vector<prmEdge> > acopy( nodo2archi ) ;

This code, which copies a map, prints the same size (1) for each.

#include <map>
#include <iostream>
using namespace std;

typedef  map <int, int> MapType;

int main() {
    MapType m1;
    m1.insert( make_pair( 1, 1 ) );
    cout << m1.size() << endl;
    MapType m2( m1 );
    cout << m2.size() << endl;
}

If your own code really doesn't copy, then I would suspect bugs in the copy constructors or the comparison functions for the contained types are screwing up memory somehow.

anon
I have tried this:map<prmNode,vector<prmEdge>,prmNodeComparator> copiamap( nodo2archi );but it doesn't work. The copiamap is empty.
livio8495
@livio Could you post the actual code?
pajton
with an int works, but with my objects doesn't work. I don't have idea why
livio8495
@livio8495 See my last para above.
anon
I have solved the problem. Thank you very much
livio8495
@livio8495 Might be nice to add a sentence or two to your answer, saying what the solution was. That way, if the problem is asked about again, we can usefully refer to this question.
anon
A: 

Does it work if instead of:

map<prmNode,vector<prmEdge>,prmNodeComparator> copiamap( nodo2archi );

You use

map<prmNode,vector<prmEdge>,prmNodeComparator> copiamap = nodo2archi;

I agree with others you are missing something somewhere, but can you test?

John
No it doesn't work with the equal.
livio8495