graph - Adjacency list c++ -



graph - Adjacency list c++ -

i'm trying create adjacency list store graph. i'm having problem accessing list 1 time create it.

class weighted_graph { private: std::vector <std::vector<std::pair<double, int>> > adjacencylist; ... weighted_graph::weighted_graph(int n) { std::vector <std::vector<std::pair<double, int>> > adjacencylist(n); (int = 0; < n; i++) { std::vector<std::pair<double, int>> row; // create empty row adjacencylist.push_back(row); } ... }

this how create list. whenever seek access in list error:

debug assertion failed! expression: vector subscript out of range

this happens whenever seek lsit, example, calling:

bool weighted_graph::insert_edge(int i, int j, double d) { if (!adjacencylist[i].empty()) {

or

bool weighted_graph::insert_edge(int i, int j, double d) { std::cout << adjacencylist[i].front().second

am creating list wrong?

one apparent issue 'adjacencylist, defined twice. first private fellow member of class, sec local variable in constructor size 'n'. therefore, when want access class fellow member other fellow member functions, refers first definition not 1 initialized in constructor. unless have particular reason doing suggest define 1 time class info fellow member , initialize in constructor dynamically set size of vector based on value of 'n' comes method argument. if try:

class weighted_graph { private: std::vector <std::vector<std::pair<double, int>> > adjacencylist; ... weighted_graph::weighted_graph(int n) { adjacencylist.resize(n); (int = 0; < n; i++) { std::vector<std::pair<double, int>> row; // create empty row adjacencylist.push_back(row); } ... }

hope helps!

c++ graph stdvector adjacency-list

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -