I’m trying to insert some pair value into a map. May map is composed by an object and a vector of another object. i don’t know why but the only way to make the code to compile is to declare the first object like a pointer. But in this way when I insert some object, only the first pair is put into the map.
My map is this:
map<prmEdge,vector<prmNode> > archi;
this is the code:
{
bool prmPlanner::insert_edge(int from,int to,int h) {
prmEdge e;
int f=from;
int t=to;
if(to<from){
f=to;
t=from;
}
e.setFrom(f);
e.setTo(t);
vector<prmNode> app;
prmNode par=nodes[e.getFrom()];
prmNode arr=nodes[e.getTo()];
app.push_back(par);
app.push_back(arr);
archi.insert(pair<prmEdge,vector<prmNode> >(e,app) );
return true;
}
}
In this way, I have an error in compilation in the class pair.h.
What could I do?? Thank you very much.
You need to supply a comparator for prmEdge. My guess is that it uses the default comparator for map, e.g. comparing the address of the key — which is always the same because
eis local.Objects that serve as Keys in the map need to be ordered, so you either need to supply a operator for comparing edges, or a comparator function for map.
The really hard part is deciding how to compare the edges so a definitive order is defined. Assuming that you only have
fromandtoYou can try with:It will sort on primary key
fromand secondaryto.