c++ 在抛出'std::out_of_range' std::vector 的实例后调用终止

我不明白为什么当我已经添加了中断循环的 if 语句时它会超出范围?

std::vector m_eachTextLine; std::string 文本 = "

for (size_t pos = 0; pos < text.length(); pos+=60)
{[&] {
    if (pos + 60 >= text.length())
    {   
        unsigned temp =  text.length() - pos;
        m_eachTextLine.push_back(text.substr(pos, temp));
        return;
    }

        m_eachTextLine.push_back(text.substr(pos,60));

}();
}



for (int i = 0; i <= m_eachTextLine.size(); i++)
{   
    std::cout << m_eachTextLine.at(i) << std::endl;
}

返回0;}

稻田

超出范围的异常是循环最后一次迭代的结果:

for (int i = 0; i <= m_eachTextLine.size(); i++)
{   
    std::cout << m_eachTextLine.at(i) << std::endl;
}

更改<=<,你会没事的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

抛出'std :: out_of_range'what():vector :: _ M_range_check实例后终止调用

抛出'std :: out_of_range'实例后调用终止

在C ++中抛出'std :: out_of_range'实例后调用终止

C++ 捕获错误“抛出'std::out_of_range'的实例”

在抛出 'std::out_of_range' what() 实例后调用终止:basic_string::at: __n 错误

比较二进制数字输入的字符时,抛出'std :: out_of_range实例后调用终止

将字母向前移动 3 个字母的程序,错误:在抛出 'std::out_of_range' 实例后调用终止

"if (argc < 2 || argc > 2)" 应该有 2 个参数吗?& 在抛出“std::out_of_range”错误实例后调用终止

为什么“在抛出一个 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0) 实例后终止调用?

在抛出 std::exception 实例后调用终止

C ++中带有矢量的'std :: out_of_range'实例

C++ 抛出错误“在抛出‘std::bad_alloc’的实例后终止调用”

std :: stoul不会抛出std :: out_of_range

C++ 合并排序实现中抛出的 std::out_of_range 异常

C ++错误:抛出'std :: bad_alloc'实例后终止调用

抛出'std::bad_alloc'的实例后调用C++终止

C ++向量:std :: out_of_range错误

抛出'std :: regex_error'实例后调用终止

在抛出“std::system_error”实例后调用 Tensorflow 终止

std::stoi() 错误——“在抛出 'std::invalid_argument' 实例后终止调用”

std :: stod抛出应为有效字符串的out_of_range错误

合并排序算法中的 C++“以未捕获的 std::out_of_range 类型异常终止:向量”错误

得到错误“抛出'std :: bad_alloc'what():whatstd :: bad_alloc实例后终止调用”

抛出“std::system_error”线程池实例后调用终止

为什么在抛出'std :: bad_alloc'实例后终止调用?

Tensorflow MNIST:抛出'std :: bad_alloc'实例后调用终止

抛出“std::invalid_argument”实例后调用终止 what(): leetcode 问题中的 stoi 错误

在抛出'std::runtime_error' what() 实例后调用终止:filebuf 和 ostream 的 I/O 错误

在抛出“std::regex_error”what() 实例后调用终止:括号未关闭