我正在编写一个在 visual studio 2012 中使用 vector 的程序。这是我的代码中有问题的片段:
std::vector< std::vector< std::vector<Eigen::Matrix3d> > > tensor;
//some code
try
{
ret = tensor[x][y][z];
}
catch(...)
{
std::cout << "Index out of range!" << std::endl;
}
当 x、y 或 z 超出范围时,程序终止而不是处理异常。
VS 中的某些设置有问题吗?
最佳答案
无论使用 vector 还是数组,使用数组索引运算符[]
索引越界时都不会抛出异常。您得到的只是未定义的行为。
如果你想要边界检查那么使用std::vector
及其 at
访问函数。
关于c++ - 无法在 visual studio 2012 中捕获 "ctor subscript out of range"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34155665/