开发者

copy a map into another map

开发者 https://www.devze.com 2022-12-23 01:44 出处:网络
I have a map like this: map<prmNode,vector<prmEdge>,prmN开发者_StackOverflow中文版odeComparator> nodo2archi;

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消