Debug Assertion Failed--vector subscript out of range

Debug Assertion Failed

Expression: vector subscript out of range

出现上面的弹窗,导致程序崩溃,如何让系统不崩溃呢。答:使用vec.at(i)。

有问题的程序

cpp 复制代码
i#include <iostream>
#include <vector>
using namespace std;

int main()
{    
    vector<int> aa(5);
    for (int i = 0; i < 6; ++i)    // 超出了vector的范围了
    {
        std::cout << aa[i] << std::endl;                // 方式1
        std::cout << *(aa.begin() + i) << std::endl;    // 方式2
        std::cout << aa.at(i) << std::endl;             // 方式3        
    }
    printf("==========\n");
    return 0;
}

出现的问题

方式一报错

方式二报错

方式三报错。

解决方案

修改后的代码,添加try-catch组合,使用第三种方式 vec.at(i)。在win中的三种方式,只有 at(n) 会抛出 std::out_of_range 异常,try-catch才能捕获到。

参考:

关于 std::vector 的下标越界检查_vector下标超出范围-CSDN博客

C++中的vector subscript out of range问题解决 - 极简博客

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;

int main()
{    
    vector<int> aa(5);
    try
    {
        for (int i = 0; i < 6; ++i)     // 超出了vector的范围了
        {
            //std::cout << aa[i] << std::endl;                // 方式1
            //std::cout << *(aa.begin() + i) << std::endl;    // 方式2
            std::cout << aa.at(i) << std::endl;             // 方式3        
        }
    }
    catch (std::out_of_range)
    {
        printf("check the range\n");
    }
    printf("==========\n");
     return 0;
}
cpp 复制代码
try {
    std::cout << vec.at(20) << std::endl;
} catch (const std::out_of_range& e) {
    std::cout << "Invalid index" << std::endl;
}

不过,最好还是在程序中,vector用vector.size() 来判断是否越界。

补充知识:

异常处理 try-catch

在C++编程中,异常处理 try-catch 是一种重要的错误管理机制,它允许程序在遇到不可预见的问题时能够优雅地恢复或报告错误,而不是直接崩溃。

基本的try-catch结构

cpp 复制代码
try {
    // 可能抛出异常的代码
    throw std::runtime_error("发生错误");
} catch (const std::exception& e) {
    // 处理异常
    std::cerr << "捕获到异常: " << e.what() << '\n';
}

当有多个catch挨着,运行了第一个catch后,后面的catch就不运行了。

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;

int main()
{    
    vector<int> aa(5);
    try
    {  
        aa.at(5) = 10;
    }
    // 当有多个catch挨着,运行了第一个catch后,后面的catch就不运行了。
    catch (const std::exception& e) 
    {       
        std::cerr << "捕获到异常: " << e.what() << '\n';  // 处理异常
    }   
    catch (std::out_of_range)
    {
        printf("check the range\n");
    }     
     return 0;
}
cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> aa(5);
    try
    {
        try { aa.at(5) = 0; }  
        catch (std::out_of_range)   // 这个catch后,后面的就不再catch了。
        {
            printf("check the range 2\n");
        }
    }
    // 当有多个catch挨着,运行了第一个catch后,后面的catch就不运行了。
    catch (const std::exception& e)
    {
        std::cerr << "捕获到异常: " << e.what() << '\n';  // 处理异常
    }
    catch (std::out_of_range)
    {
        printf("check the range 1\n");
    }
    return 0;
}
相关推荐
微信公众号:AI创造财富18 分钟前
conda create -n modelscope python=3.8 conda: command not found
开发语言·python·conda
鱼会上树cy18 分钟前
空间解析几何10:三维圆弧拟合【附MATLAB代码】
开发语言·matlab
IT艺术家-rookie1 小时前
golang--channel的关键特性和行为
开发语言·后端·golang
凌肖战1 小时前
力扣网C语言编程题:三数之和
c语言·算法·leetcode
心软且酷丶3 小时前
leetcode:263. 丑数(python3解法,数学相关算法题)
python·算法·leetcode
我最厉害。,。3 小时前
C2远控篇&C&C++&ShellCode分离&File提取&Http协议&Argv参数&Sock管道
c语言·c++·http
Cyrus_柯3 小时前
C++(面向对象编程——关键字)
开发语言·c++·算法·面向对象
大龄Python青年3 小时前
C语言 函数怎样通过数组来返回多个值
c语言·开发语言
LQYYDSY3 小时前
【C语言极简自学笔记】重讲运算符
c语言·开发语言·笔记