用 at() 取空栈会抛什么

👁️ 1 人浏览 💬 0 人评论 ❤️ 添加收藏

运行下面这段程序,它把捕获到的异常类型打出来。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 编译。)

提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论