0

So I was learning about the stlibrary and graphs, and so I found out that graphs could be represented as a vector of lists which could be like this, where the 1 2 3 4 5 6 are the vertices, and from the vertice 1 I could go to the number 2, from the 3 to the 6, etc.

1 2 3 4 5 6 
2   6 1   2
      2

But, I already saved these values in the vector list, how could I loop through it to get the graph? My vector list is called _verticesEdges.

Like, to get an output like this:

Vertice 1: 2

Vertice 2:

Vertice 3: 6

Vertice 4: 1 2

Vertice 5:

Vertice 6: 2

Appreciate your help!

magalenyo
  • 275
  • 6
  • 17
  • 1
    Note that [`stl != std`](http://stackoverflow.com/questions/5205491/whats-the-difference-between-stl-and-c-standard-library). You will have to show some code to get help. Please provide a [mcve]. – 463035818_is_not_an_ai Nov 29 '16 at 09:56
  • I'd search for how to iterate through a vector. Also, as @tobi303 said, you need to show some code here to get help. – Vada Poché Nov 29 '16 at 10:08

2 Answers2

1
Assuming you have stored from index 1 to n (that means size of 0th index of your vector is zero), where n is number of vertices,

for (int i = 1; i <= n; i++)
{
  cout << "Vertex " << i << ": ";

  for (int j=0; j< _verticesEdges[i].size(); j++)
  cout <<  _verticesEdges[i][j] << " ";
  cout << "\n";
}
dj_1993
  • 93
  • 1
  • 8
0

Something like this

std::vector<std::list<int>> vecOfLists;
// fill vecOfLists;
for (size_t i = 0; i < vecOfLists.size(); ++i) {
    std::cout << "Vertice " << i + 1 << ": ";
    for (int num : vecOfLists[i]) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
}

I am using usual for for iterating through lists, since index is required, and using range-based for for iterating through list, since this is better and modern way to iterate through whole container if you don't require indexes.

Starl1ght
  • 4,422
  • 1
  • 21
  • 49