small question vector <list>

Hello, this is just small question.

Thanks!

How can push data into the vector <list<Edge>> adjList?
adjList[n].push_back (e);//gives error.

1
2
3
4
5
6
7
8
9
10
struct Edge {
	int from_node_number;
	int to_node_number;
	int weight;
 };
vector <list<Edge>> adjList; 
Edge e;

for (int n=0; n< graph.size(); n++) 
adjList[n].push_back (e);
adjList[n] where n is any number will fail, since adjList is empty.
I am loading some data before...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Node {
    int number;
	
 };
struct Node node;
vector<Node> graph;
	
struct Edge {
	int from_node_number;
	int to_node_number;
	int weight;
 };
vector <list<Edge>> adjList; 
Edge e;

for (int n=0; n< graph.size(); n++) 
e.from_node_number = graph[n].number;
adjList[n].push_back (e);
adjList[n] where n is any number will fail, since adjList is empty.

adjList is still empty here.
hm...do I need to resize it before? Thanks.
If you intend for adjList to be in parallel with graph (ie. adjList[n] is the adjacency list for graph[n]), that's what I would do. There are multiple problems with the code as posted, but I assume that's because you've attempted to leave things out to simplify the issue.
yes, put just a small part here. Thanks. It worked after I resized adjList to amount of edges.
Topic archived. No new replies allowed.