我在玩 C++ 中的向量向量。在我的情况下,我称之为 3D 向量的内容显示在以下代码中
typedef std::vector<double> RandomSample;
typedef std::vector<RandomSample> TimeSample;
typedef std::vector<TimeSample> Option;
int main(int argc, const char * argv[])
{
unsigned int numberOfOptions = 3;
unsigned int timeNodes = 7;
unsigned int numberOfRandSamples = 10;
Option options(3, TimeSample(7, RandomSample(numberOfRandSamples)));
std::cout << options[0][0][0] << std::endl;
//std::cout << options[3][6][9] << std::endl; //SEGMENTATION FAULT
//std::cout << options[2][7][9] << std::endl; //SEGMENTATION FAULT
std::cout << options[2][6][20] << std::endl; //NO ERROR !!
std::cout << "Hola Mundo !" << std::endl;
return 0;
}
代码本身就说明了问题,当访问超出第一个和第二个索引的向量边界时,我得到了预期的运行时错误,但是当对第三个索引执行相同操作时,它不会发生,没有错误,什么都没有。我什至尝试过在第三个索引中使用大数字,但显然一切都运行良好。我错过了什么或这段代码发生了什么?
我正在 Mac OS X 10.8.4 + Xcode 4.6.3 上开发