用 at() 取空栈会抛什么
运行下面这段程序,它把捕获到的异常类型打出来。at() 会检查下标,越界就抛异常:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include <stdexcept>
int main() {
vector<string> st;
try {
cout << st.at(0) << endl;
} catch (const out_of_range& e) {
cout << "out_of_range" << endl;
}
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论