c++ vector elements access -


i trying create vector datatype have defined in class

vector<mydatatype> myvector; 

but when try access elements in vector using loop

for(int i=0; i<myvector.size();i++) 

i error message telling vector out of range debug assertion failed line 932!

this code

vector <road_segment> unvisited;//the vector  (i = 0; i<unvisited.size(); i++) {     cur_node = unvisited[0];//current node visit      find_node_neighbers(cur_node.end_station.id, end, t_r); 

when try comment loop don't error message

any appreciated

this line:

cur_node = unvisited[0]; 

should be:

cur_node = unvisited[i]; 

otherwise, access first element on , over.

but if need perform action on each element, should use foreach loop instead:

for (const road_segment& cur_node : unvisited) {   // i'm guessing "neighbers" typo, that's in question   find_node_neighbers(cur_node.end_station.id, end, t_r); } 

this allows avoid problems indices , such. or if need modify each element instead of using it, remove const:

for (road_segment& cur_node : unvisited) {   // modifies cur_node } 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -