I have a 4d vector and I am trying to add values to it. I don’t receive any compiler errors, but when running it, the program freezes when it gets to the line where I am adding values to this vector.
I initialize the vector in a header file like this:
std::vector<std::vector<std::vector<std::vector<unsigned int> > > > _celllist;
The array is then sized with the following:
_celllist.resize(_vnx);
for(int i=0;i<_vnx;i++)
{
//y axis size
_celllist[i].resize(_vny);
for(int j=0;j<_vny;j++)
{
//z axis size
_celllist[i][j].resize(_vnz);
}
}
This line then causes the program to crash:
_celllist[ix][iy][iz].push_back(i);
Note that ix, iy, and iz are all int and i is an unsigned int.
Can anyone see what might be going wrong here? Thanks
The operator[] does not expand or guarantee that the element is valid. It’s a faster version of at() with little or no error checking:
Convert your code to the safer version for now:
This new code will throw an out_of_range exception if your indices are off.