首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

容器程序为何异常?

2012-09-15 
容器程序为何错误???C/C++ codevect3[i].resize(5+1)for(int j1j5+1j++){vect3[i][j][0]x2++//你看

容器程序为何错误???

C/C++ code
vect3[i].resize(5+1);  for(int j=1;j<5+1;j++)  {   vect3[i][j][0]=x2++;//你看我有j=1开始循环,而不是0,因为我要vect3[0][1][0]  vect3[0][2][0]记录一些数据,必须在这调用赋值,上面我已经申请了vect3[2][2][2], vect3[i][j][0]怎么还不行?   vect3[i][j].resize(3+1); 


C/C++ code
#include <vector>#include <iostream>using namespace std;int main (){ int x=0; int x2=0; int x3=0; int Third=2; int Second=2; int Firth=2; vector<vector<vector<int>      > >    vect3(Third,vector<vector<int>      >         (Second,vector<int>(Firth))); vect3.resize(2); for(int i=0;i<2;i++) {  vect3[i][0][0]=x++;  vect3[i].resize(5+1);  for(int j=1;j<5+1;j++)  {   vect3[i][j][0]=x2++;   vect3[i][j].resize(3+1);     for(int k=1;k<3+1;k++)    vect3[i][j][k] =x3++;  } }}


[解决办法]
VC200x的IDE很牛叉的,能够直接看容器中的元素的值。
不设断点,直接调你的程序,挂了之后,翻堆栈帧至main函数,鼠标指向vect3,这时IDE出现一个窗口,图我不会截,就一行内容:
vect3 [2]([6]([2](0,0),[4](0,0,1,2),[0](),[0](),[0](),[0]()),[2]([2](0,0),[2](0,0)))
将鼠标移到最左边的加号上,自动再显示两行,其中第一行为
[0][6]([2](0,0),[4](0,0,1,2),[0](),[0](),[0](),[0]())
再将鼠标移到最左边的加号上,什么都一目了然了。
因为这个时候,i为0,j为2,你最开始申请不够多,所以挂了。


把vect3[i][j].resize(3+1);往上提一行,看下行不行?

热点排行