Popping off vector.

A while ago i post a thread about greedy function.

So let's say there's a symmetric matrix that represent the distant between each city. I am to write a function that connects the NEAREST (not the shortest path possible but the nearest) path. Well there's a problem, the problem is if i write a min search for each row, it will just goes from city A to city B back to city A. To solve that problem godpyro suggest me every time i finish a min search, i "pop" it off. That being said i assume is pop the city off. So if my first route is from city A to B, A will be pop off in the second row min search. M question is how to do that?

The thing is the way i store my data looks like this
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
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;
}


Is there a better way to store the data? And how to "pop" it off.
Last edited on
right off the top of my head, if you use a list<> instead of a vector<>, you would find it easier to erase any path.

also, you would need keep track of symmetric-paths (A to B and B to A) so when you delete one, you could also delete the other.

perhaps you could use a higher level abstraction called Map which keeps track of all paths and have this Map hold on to your City's.
Topic archived. No new replies allowed.