This is my code:
#include <iostream>
using namespace std;
struct nodos
{
string nombre; //Nombre del nodo
string vecinos[]; //Los otros nodos del grafo
float aristas[]; //Valores de las aristas
};
int main()
{
int numnodos;
cout<<"Ingrese la cantidad de nodos a trabajar: ";
cin>>numnodos;
cout<<"\n";
nodos arreglodenodos[numnodos]; //Declaro un arreglo de estructuras
return 0;
}
I am making a program on graphs but when I wanted to program the nodes (vertices) I have run into the following problem:
- For example, enter 5 in the number of nodes to work.
So the structure array will now have a size of 5.
but here comes my problem
Obviously the number of neighbors will be
numnodos-1
- But I don't know how to assign that size to the array
vecinos[]
also witharistas[]
PS: as a bonus question. Would it be better to declare nodes as a class
or leave it as a struct
?
Without making many changes to your code you can try the following:
We use
template <int N>
and use it like this:Where 5 is the amount used for the template. Note that
N
it is used inside the struct to assign the sizestring vecinos[N-1];
orfloat aristas[N];
.What is under
// Solo para test
, is not relevant in your question, this line:sizeof(arreglodenodos[1].vecinos)/sizeof(arreglodenodos[1].vecinos[0])
It is to calculate the size.
ideone
Another way if the constant is a problem would be the following, but you have to make a few changes:
ideone
std::vector info:
I would use the classes and lists (instead of arr) for this work since there should be an edge obj since this is the one that indicates which are the nodes that are related and the node indicates which are the edges that relate them, something like that :
class Node { String name; List list_edge; //Constructor , get and set ... } class Edge{ String value; Node n1; Node n2; }