linking links and identifier

3 Cities A, B, C
and let this 3 by 3 matrix represent the distant between them


int distant[3][3]=
{0 , 1 , 2;
1 , 0, 3;
2 , 3, 0}

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.
Last edited on
Would something like this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct Path {
  City destination;
  int distance;
};

struct City {
  vector<Path> neightbors;
  ... //other members (fields, methods, ...)

  void addPath(City dest, int dist) {
    Path path = { dest, dist };
    neightbors.push_back(path);
  }
};

int main() {
  City A, B, C;
  A.addPath(B, 1);
  A.addPath(C, 2);
  B.addPath(A, 1);
  B.addPath(C, 3);
  C.addPath(A, 2);
  C.addPath(B, 3);

  ... //do stuff with the cities

  return 0;
}
Last edited on
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?
Last edited on
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! ;)
I don't really get what you mean userulluipsete,

in your enum Cities {A, B, C}; //A = 0 B = 1, C = 2 ok i understand that
but what baout intdistancecb = distant[c][b]//i don't get it

i thought of declaring a 3 by 3 array

int distant[3][3]=
{0 , 1 , 2;
1 , 0, 3;
2 , 3, 0}

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.
Carefully, Jimmy, carefully!

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?
Well, yea i don't need the vector there, let me rephrase my question, my question is now that you define it

1
2
3
4
5
6
A.addPath(B, 1);
  A.addPath(C, 2);
  B.addPath(A, 1);
  B.addPath(C, 3);
  C.addPath(A, 2);
  C.addPath(B, 3);


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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
void TrackCity (Cities currentCity)
{
  int nCounter;
  list <Cities>::iterator it;
  bool bContinueRecursively;

  
  beenThere.push_front(currentCity);

  for (nCounter = CITY_A; nCounter < NR_OF_CITIES; nCounter++)
  {
    if (vDistances[currentCity][nCouter] > 0)  
    {
      bContinueRecursively = true;  

      for (it = beenThere.begin(); it != beenThere.end(); it++)
        if (currentCity == *it)
          bContinueRecursively = false;  

      if (bContinueRecursively == true)
      {
        TrackCity(nCouter);  
      }
      else  //nowhere to go, so consider remembering the path
      {
        if (beenThere.size() > finalRouteResult.size())
          finalRouteResult = beenThere;
      }
    }
  }

  beenThere.pop_front();
}
struct City; 
struct Path {
  City destination;
  int distance;
};

struct City {
  vector<Path> neightbors;
  void greedyfunc(array1, array2);//this is one of the memebers i am putting
  int passes;

  void addPath(City dest, int dist) {
    Path path = { dest, dist };
    neightbors.push_back(path);
  }
};

int main (void)
{
int main() {
  City A, B, C;
  A.addPath(B, 1);
  A.addPath(C, 2);
  B.addPath(A, 1);
  B.addPath(C, 3);
  C.addPath(A, 2);
  C.addPath(B, 3);;
}



Topic archived. No new replies allowed.