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;
}
相关推荐
云泽80812 分钟前
C/C++内存管理详解:从基础原理到自定义内存池原理
java·c语言·c++
weixin_3077791315 分钟前
在Linux服务器上使用Jenkins和Poetry实现Python项目自动化
linux·开发语言·python·自动化·jenkins
润 下15 分钟前
C语言——深入解析C语言指针:从基础到实践从入门到精通(四)
c语言·开发语言·人工智能·经验分享·笔记·程序人生·其他
cyclel17 分钟前
散列表的小想法
算法
Empty_77719 分钟前
Python编程之常用模块
开发语言·网络·python
Code小翊21 分钟前
堆的基础操作,C语言示例
java·数据结构·算法
余俊晖21 分钟前
如何让多模态大模型学会“自动思考”-R-4B训练框架核心设计与训练方法
人工智能·算法·机器学习
小火柴12323 分钟前
利用R绘制箱线图
开发语言·r语言
Emilia486.28 分钟前
【Leetcode&nowcode&数据结构】顺序表的应用
数据结构·算法·leetcode
一水鉴天33 分钟前
整体设计 逻辑系统程序 之27 拼语言整体设计 9 套程序架构优化与核心组件(CNN 改造框架 / Slave/Supervisor/ 数学工具)协同设计
人工智能·算法