I have a map like this:
map<prmNode,vector<prmEdge>,prmN开发者_StackOverflow中文版odeComparator> 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
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.
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?
精彩评论