C++ 在抛出 'std::out_of_range' what() 实例后调用终止:basic_string::substr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22130158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:57:04  来源:igfitidea点击:

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr

c++substr

提问by inyourmind

I received this error: "terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr"from this code:

我收到了这个错误:“在从这段代码中抛出一个 'std::out_of_range' what(): basic_string::substr 实例后调用了终止”

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>

using namespace std;

vector <string> n_cartelle;

ifstream in("n_cartelle.txt");
string linea;

while(getline(in,linea))
n_cartelle.push_back(linea);


for(int i=0; i < 4; ++i){


if(n_cartelle[i].substr(n_cartelle[i].size()-3) == "txt")
cout <<"si"<<endl;
else
cout<<"no"<<endl;

}

If I try with this:

如果我尝试这样做:

if(n_cartelle[7].substr(n_cartelle[7].size()-3) == "txt")
cout <<"si "<<n_cartelle[7]<<endl;
else
cout<<"no"<<endl;

I don't get the error.

我不明白这个错误。

回答by Christian Hackl

What you experience is probably a case of an exception falling off main(), which terminates the program and gives an OS-specific error message like the one you posted.

您所经历的可能是异常脱落的情况main(),这会终止程序并给出与您发布的类似的特定于操作系统的错误消息。

As a first measure, you can catch exceptions in main(). This will prevent your program from crashing.

作为第一个措施,您可以在main(). 这将防止您的程序崩溃。

#include <exception>
#include <iostream>

int main()
{
    try
    {
        // do stuff
    }
    catch (std::exception const &exc)
    {
        std::cerr << "Exception caught " << exc.what() << "\n";
    }
    catch (...)
    {
        std::cerr << "Unknown exception caught\n";
    }
}

Now that you have this mechanism in place, you can actually hunt down the error.

既然你已经有了这个机制,你就可以真正找到错误了。

Looking at your code, it couldbe that n_cartellehas less than 4 elements, caused perhaps by n_cartelle.txt containing only 3 lines. This would mean that n_cartelle[0], n_cartelle[1]and n_cartelle[2]would be fine, but trying to access n_cartelle[3]and anything beyond would be undefined behaviour, which basically means that anythingcan happen. So first make sure that n_cartelleactually has 4 elements and that your program has defined behaviour.

看你的代码,它可以n_cartelle具有小于4种元素,含有只有3线n_cartelle.txt可能引起的。这意味着n_cartelle[0],n_cartelle[1]并且n_cartelle[2]会很好,但是尝试访问n_cartelle[3]和任何超出的行为都是未定义的行为,这基本上意味着任何事情都可能发生。所以首先确保它n_cartelle实际上有 4 个元素并且你的程序已经定义了行为。

The next thing which could go wrong (more likely, to be honest) is your substr()call. When you try to call substr()with "impossible" arguments, for example getting the substring starting at character 10 of a string containing only 5 characters, then the behaviour is a defined error- a std::out_of_rangeexception is thrown. The same happens (indirectly, almost every time) when you accidentally try to pass a negative number as the first argument of substr(). Due to the internal workings of a std::string, a negative number would be converted to a huge positive number, certainly much longer than the string, and result in the same std::out_of_rangeexception.

下一件可能出错的事情(说实话更有可能)是你的substr()电话。当您尝试substr()使用“不可能的”参数进行调用时,例如获取从仅包含 5 个字符的字符串的第 10 个字符开始的子字符串,则行为是定义的错误-std::out_of_range抛出异常。当您不小心尝试将负数作为substr(). 由于 a 的内部工作原理std::string,负数将被转换为一个巨大的正数,肯定比字符串长得多,并导致相同的std::out_of_range异常。

So, if one of your lines has a length less than 3 characters, size() - 3is negative and what I just explained happens.

因此,如果您的其中一行的长度小于 3 个字符,size() - 3则为负数,并且我刚刚解释的内容会发生。