【发布时间】:2023-04-03 02:45:01
【问题描述】:
vector<int> input = {1, 2, 3, 4, 17, 117, 517, 997};
cout<< "input vector at index -1 is: " << input[-1] <<endl;
使用上面的代码,结果将是:索引-1处的输入是:0。 但是,如果我们使用以下方法:
vector<int> input = {1, 2, 3, 4, 17, 117, 517, 997};
cout<< "input vector at index -1 is: " << input.at(-1) <<endl;
结果将是: 索引 -1 处的输入是:libc++abi.dylib: 以 std::out_of_range: 向量类型的未捕获异常终止。
谁能给我解释一下原因?谢谢。
【问题讨论】:
标签: c++ c++11 vector stl indexoutofboundsexception