visual studio 2010 - c++ vector causes array subscript error. Debugger shows error CXX0004 -


i implementing floyd-warshall algorithm in ms vs 2010. using vector container. these seemingly innocent lines:

#include <iostream> #include <iomanip> #include <vector> #include <fstream> #include <string> #include <limits> using namespace std;  const double inf = numeric_limits<double>::infinity();  struct node{     int value;     //path not used here     //node *predecessor; };  struct edge{     int source;     int target;     double weight; };  vector<edge> edges; 

cause problem visible @ debugger when stop @ breakpoint @ start of main() body: instead of having edges beautiful menu should, red exclamination mark @ name, , cxx0004: error: syntax error. trying run program causes debug assertion: vector subscript out of range. exact same lines work fine in similar program i've made. what's wrong?

here whole code:

#include <iostream> #include <iomanip> #include <vector> #include <fstream> #include <string> #include <limits> using namespace std;  const double inf = numeric_limits<double>::infinity();  struct node{     int value;     //path not used here     //node *predecessor; };  struct edge{     int source;     int target;     double weight; };  vector<edge> edges; vector<node> v; vector< vector< vector< double > > > r; int u;//source  unsigned ecount,vcount;  void insert_from_keyboard(void) {     unsigned i,j;     cout<<"keyboard insertion selected. enter vertex count: ";     cin>>vcount;     v.resize(vcount);     for(i=0;i<vcount;i++)         v[i].value=i+1;//naming nodes, start 1     r.resize(vcount+1);     for(i=0;i<vcount+1;i++)     {         r[i].resize(vcount);         for(j=0;j<vcount;j++)             r[i][j].resize(vcount,inf);     }     cout<<"nodes 1 "<<vcount<<" created.\nenter edge count: ";     cin>>ecount;     edges.resize(ecount);     cout<<"enter "<<ecount<<" triplets representing directed edges (source, target, weight): ";     for(i=0;i<ecount;i++)         cin>>edges[i].source>>edges[i].target>>edges[i].weight;     return; }  bool floyd_warshall(void) {     unsigned i,j,k;      for(i=0;i<vcount;i++)         r[0][i][i] = 0;     for(i=0;i<ecount;i++)         r[0][edges[i].source-1][edges[i].target-1] = edges[i].weight;      for(i=1;i<vcount+1;i++)         for(j=0;j<vcount;j++)             for(k=0;k<vcount;k++)                 r[i][j][k]= r[i-1][j][k] > r[i-1][j][i] + r[i-1][i][k] ? r[i-1][j][i] + r[i-1][i][k] : r[i-1][j][k];       for(i=0;i<vcount;i++)         for(j=0;j<vcount;j++)             if ( r[i][j][j]<0 )                 return false;     return true; }  void printr() {     //printing routine, should not bother....     //the log function used calculate maximum length of numbers, used formatting...     unsigned l,i,j, wl=5, wd=8;//width printing whitespacedges l l(...), d data     for(l=0;l<vcount+1;l++)     {          cout<<"r("<<setw(wl)<<l+1<<"):\n\n";         for(i=0;i<vcount;i++)         {             cout<<setw(5+wl+wd/2)<<r[l][i][0];             for(j=1;j<vcount;j++)                 cout<<setw(wd)<<r[l][i][j];             cout<<endl;         }         cout<<"\n\n";     } }  void insert_from_file(string filename) {     unsigned i,j;     ifstream f (filename.c_str());     f>>vcount>>ecount;     v.resize(vcount);     for(i=0;i<vcount;i++)         v[i].value=i+1;//naming nodes, start 1     r.resize(vcount+1);     for(i=0;i<vcount+1;i++)     {         r[i].resize(vcount);         for(j=0;j<vcount;j++)             r[i][j].resize(vcount,inf);     }     edges.resize(ecount);     for(i=0;i<ecount;i++)         f>>edges[i].source>>edges[i].target>>edges[i].weight;     f.close();     cout<<"insert file "<<filename<<" successful\n\n";  }  int main(void) {     //insert_from_keyboard();     insert_from_file("a22.txt");     bool result =floyd_warshall();     printr();     cout<<"result of algorithm: "<<(result ? "true, no negative" : "false, negative" )<<" cycles encountered.\n";     system("pause");     return 0; } 


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -