as you see the diagonal is zero that is because distance is 0 from city A to city A (which means u don't move). now my question is how to make a link between those city and the distance of the city? When i say "link" i mean in c++ code, of course if i say it in words you will say "oh A B C is the cities, and the matrix is the distance between each city" but how to make the code to recognize it for future program possibility.
that is almost/exactly what i want, but instead i would like to see something like
A.addPath(B) = 1; is it possible? Or even better A.addPath(B,1) = array[1][2] "storing one in first row second column"
And what is vector<Path> i never learn it mind giving me the link to the tutorial?
vector is one of the STL structures. You may look here for reference. http://www.cplusplus.com/reference/stl/vector
But to understand it, you have to know data types including structures, and templates.
About your actual problem, you don't need vectors, especially if you already have an array of values like the one you declared. It is called look-up table. The way you defined it, you can just retrieve any distance between registered cities by making a correspondence between cities and array indexes. An example would be enum Cities {A, B, C};
and the distances can be accessed as: int distanceCB = distant[C][B];
...and btw, when declaring arrays, don't put there semicolons! ;)
and write a for loop
for (int i = 0; i < 3;i++)
{
A[i] = distant [i][1];
}
but then again i just make A[3] = {0,1,2}; i didn't make A with respect to B = 1, A with respect to C = 2.
First, you don't want to have A[i] =distant [i][1];
after defining the enumeration enum Cities {A, B, C };
And second, when I said that you don't need vectors, what I meant is that you don't resort to vectors at all. This is like not having "A[i] = //...wherever", because vectors are dynamic structures, and your value assignment from some static-declared array is simply redundant and a misuse of vector's dynamic functionality. If we'll take Mathhead200's "struct City" as reference model, do we really need that vector member in it?
Now remember the post you posted on "passing the route only once?" userulluipeste, the reason why i would like to store those in array is because i would like to combine it(i mean combine this post and the other post). Where i define the distant AND be able to check i only pass the route once, so that's why i want it to be an array, since the "passing the route only once" uses array to check.
I used vectors to make it more versatile/reusable. By not using vectors you assume all the cities have exactly the same number of neighboring cities, which is fine if that's what you want. You don't really loss anything by using vectors though. Anyway, to combine with your other post:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
struct City; //I realized you would need to forward declare City
struct Path {
City destination;
int distance;
};
struct City {
vector<Path> neightbors;
... //other members (fields, methods, ...)
// one of them is
int passes; //how many times the city was passed
void addPath(City dest, int dist) {
Path path = { dest, dist };
neightbors.push_back(path);
}
};
how you combine this with my other post?(although that's my intention) as you see i am really not firmilar with vector so here's what i will put (for now i know it doesn't work that is becuase i am still learning iterations and vector, there are some modification i need to make on your code and userulluipeste's code).