[英]terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr
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. 我没有得到错误。
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()
. 首先,您可以在
main()
捕获异常。 This will prevent your program from crashing. 这将防止您的程序崩溃。
#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 could be that n_cartelle
has less than 4 elements, caused perhaps by n_cartelle.txt containing only 3 lines. 查看您的代码, 可能是
n_cartelle
元素少于4个,可能是n_cartelle.txt仅包含3行引起的。 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 anything can happen. 这意味着
n_cartelle[0]
, n_cartelle[1]
和n_cartelle[2]
会很好,但是尝试访问n_cartelle[3]
以及超出此范围的任何东西都是不确定的行为,这基本上意味着任何事情都会发生。 So first make sure that n_cartelle
actually has 4 elements and that your program has defined behaviour. 因此,首先要确保
n_cartelle
实际上具有4个元素,并且您的程序已定义了行为。
The next thing which could go wrong (more likely, to be honest) is your substr()
call. 接下来可能出错的地方(老实说,很有可能是您的
substr()
调用)。 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_range
exception is thrown. 当您尝试使用“不可能的”参数调用
substr()
时,例如,使子字符串从仅包含5个字符的字符串的字符10开始,则该行为是已定义的错误 -引发std::out_of_range
异常。 The same happens (indirectly, almost every time) when you accidentally try to pass a negative number as the first argument of substr()
. 当您不小心尝试将负数作为
substr()
的第一个参数传递时,也会发生这种情况(几乎每次都是间接发生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_range
exception. 由于
std::string
的内部工作原理,负数将转换为巨大的正数,肯定比字符串长得多,并导致相同的std::out_of_range
异常。
So, if one of your lines has a length less than 3 characters, size() - 3
is negative and what I just explained happens. 因此,如果您的一行内容的长度少于3个字符,则
size() - 3
为负数,而我刚才解释的情况就发生了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.