When reading from vector I put values in I got zero size. I have:
class Graph {
public:
vector<Vertex> vertices;
};
class Vertex {
public:
vector<int> adjacentVertices;
};
In my load method then:
int vertices, edges;
cin >> vertices >> edges;
Graph mainGraph;
mainGraph.vertices.reserve(static_cast<unsigned int>(vertices));
int tmp1, tmp2;
for (int i = 0; i < edges; i++) {
cin >> tmp1 >> tmp2;
mainGraph.vertices[tmp1].adjacentVertices.push_back(tmp2);
cout << mainGraph.vertices[tmp1].adjacentVertices.size(); //PRINTS NUMBERS -> SEEMS OKAY
}
cout << mainGraph.vertices.size(); //IS ZERO???
for(const Vertex &v : mainGraph.vertices){ //CRASHES
cout << v.adjacentVertices.size();
}
I bet this is very stupid but what am I missing? I read the vector will self construct itself upon use if no special constructor is needed.