I've been trying to create a directed graph following https://www.youtube.com/watch?v=V_TulH374hw
class Digraph {
public:
Digraph();
void addNode(Node);
void addEdge(Edge);
void print();
private:
//This is a vector which contains a node source and a vector of node destinations
vector< tuple< Node, vector<Node>>> nodes;
};
but after I add 2 nodes and a Edge it seems like the vector with the destinations is beeing emptied
void Digraph::addEdge(Edge e){
Node src = e.getSrc();
Node dest = e.getDest();
for(auto node : nodes){
if(get<0>(node).getName() == src.getName()){
get<1>(node).push_back(dest);
//cout << "added conection " << get<0>(node).getName() << " -> " << get<1>(node).back().getName() << " now " << get<0>(node).getName() << " has " << get<1>(node).size() << " destinations" <<"\n";
return;
}
}
cout << "node " << src.getName() << " does not exist \n";
return;
}
void Digraph::print(){
for(auto node : nodes){
get<0>(node).print();
cout << " has " << get<1>(node).size() << " destinations";
cout << "\n";
for(auto destination : get<1>(node)){
cout << "\t->";
destination.print();
cout << "\n";
}
}
}
In main.cpp I add the nodes and the edge
graph.addNode(NY);
graph.addNode(CHICAGO);
graph.addEdge(road);
graph.print();
It ends up adding the edge succesfully but when it prints the final result it does not recognize the edge it just added
added conection NY -> Chicago now NY has 1 destinations
NY has 0 destinations
Chicago has 0 destinations
When tried with more Nodes and Edges I realized it never adds more than one Edge, maybe it has to do with how i defined the class? vectors is not the choice?