c++ - How to iterate through a vector of nodes -


i implementing dijkstra's shortest path algorithm. have made node class follows:

node::node(int to,int weight) {     this->to = to;     this->weight = weight; } 

i have made graph class :

class graph {     int v,e;     vector<node> *adj;     public :         graph(int v,int e);         void addedge(int v,int w,int weight);         void dijkstra(int src,int v); };  graph::graph(int v,int e) {     this->v = v;     this->e = e; }  void graph::addedge(int v,int w,int weight) {     node x(w,weight);     adj[v].push_back(x);     node y(v,weight);     adj[w].push_back(y); } 

now in dijkstra's algorithm function, want iterate through adjacency list (here vector):

void graph::dijkstra(int src,int v) {     struct heap_node node[v];     for(int i=0;i<v;i++)     {         node[i].vertex_value = int_max;         node[i].vertex_no = i;     }     bool visited[v];     memset(visited,false,sizeof(visited));     node[src].vertex_value = 0;     make_heap(node,node+v,compare);     //as have set vertex_value(distance source) of source node 0,we left v-1 vertices. so, run loop.      for(int i=0;i<v-1;i++)       {         pop_heap(node,node-i-1,compare);         //this extract minimum heap , set last position.          int cur = v-i-1;         int u = node[cur].vertex_no;         visited[u] = true;         vector<node>::iterator it;         for(it = adj[u].begin() ; != adj[u].end() ; it++)         {             node v = *it;         }     } } 

but gives me following errors:

dijkstra1.cpp: in member function ‘void graph::dijkstra(int, int)’: dijkstra1.cpp:79:10: error: ‘node’ cannot appear in constant-expression vector::iterator it;

dijkstra1.cpp:79:14: error: template argument 1 invalid vector::iterator it;

dijkstra1.cpp:79:14: error: template argument 2 invalid

dijkstra1.cpp:79:26: error: expected initializer before ‘it’ vector::iterator it;

dijkstra1.cpp:80:7: error: ‘it’ not declared in scope for(it = adj[u].begin() ; != adj[u].end() ; it++)

how rid of that.

looks have 2 types: heap_node, , node class. i'm not sure type adj, either way, cannot make vector type instance. has type. either make it

typedef struct heap_node heap_node_struct vector<heap_node_struct>  

or c hange heap_node struct node

heap_node struct the_heap_node; 

so not hide original node class.


Comments