c++ - a digraph representation by structs -


I want to make a diagram of destination node and original node in each edge . In addition, each node has information about the incoming exit and outgoing emails .

I have a surround matrix [] [] and I want to read the matrix and create a new graph.

Here's the code;

  Structure node {vector & lt; Edge * & gt; IncomingEdges; Vector & lt; Edge * & gt; OutgoingEdges; }; (Int i = 0; i & lt; total node for the root edge (frame node destination; node origin;};   

...

  ; I ++) {struct node node;}   

... () (int i = 0; i & lt; total node; i ++) {For (int j = 0; j and lt; total nodes; J ++) {if (adjacency matrix [i] [j] == 1) {struct edge edge; edgeij.destination = nodej; kinageri korig = Node; nodei- & gt; outgoing.push_back (edgeij); // ??? nodej- & gt; incoming.push_back (edgeij); //}}}

I'm not familiar with C / C ++ Clearly I have some help to improve this How can I push_back a vector of the declared declaration in the struct?

when you do :

  if (proximity matrix [i] [j] == 1) {struct edge edge; edgeij.destination = nodej; kinaregi.corig = nodi; nodei-> outgoing.push_back (Edgeij); // ??? nodej- & gt; incoming.push_back (edgeij); // ???}   

edge at the end of the block The object is destroyed, which makes it very useless (and the code is not compiled anyway, because you push it Trying the object where pointer is expected)

Since your vector is expecting the indicator, you use the dynamically allocated edge Should:

  edge * edge = new border; Edgeij.destination = nodej; Kinaregi Korig = nodi; Nodei-> Outgoing.push_back (edgeij); Nodej- & gt; Incoming.push_back (edgeij); // Now, do not delete resources allocated with the new   

, to better design solutions, to completely avoid raw pointers, and smart pointers to choose, e.g. std :: vector & lt; Std :: unique_ptr & lt; Node & gt; & Gt; .

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -