I was looking at a basic implementation of a graph with an adjacency list, and I noticed to represent the addition of an edge between two nodes, push_back
could be used multiple times on a single index. I tried it myself with this:
int V = 3;
vector<int> vec[V];
vec[0].push_back(23);
vec[0].push_back(24);
vec[1].push_back(52);
vec[2].push_back(4);
vec[2].push_back(-1);
I printed them out and it all worked fine:
for(int i=0; i<V; ++i) {
for(auto p : vec[i])
cout << p << ' ';
cout << '\n';
}
How come multiple p
can come from the same index i
? Am I over/under-thinking something and this behavior should be expected? I thought that a vector was just an array that could change size, not that one of its positions could hold many elements.