尝试运行程序时出现 C++ std::out_of_range 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6203598/
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
C++ std::out_of_range error when I try to run the program
提问by samuraiseoul
Okay so first off, Im pretty new to programming, Ive only read a bit of stuff and have been working on some project Euler problems to kind of wrap my head around concepts and such. However, I got an error message today that I couldn't make any sense of so I thought I would ask here for some help! Any links or advice is appreciated!
好的,首先,我对编程还很陌生,我只阅读了一些东西,并且一直在研究一些项目欧拉问题,以解决概念等问题。但是,今天我收到一条错误消息,我无法理解,所以我想我会在这里寻求帮助!任何链接或建议表示赞赏!
Here's the error message:
这是错误消息:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr Aborted
So any advice you might have would be awesome! If you need to see my code or have questions, ask! Though I''d rather try to understand the problem then find the answer myself! Thanks!
所以你的任何建议都会很棒!如果您需要查看我的代码或有疑问,请提问!虽然我宁愿尝试理解问题然后自己找到答案!谢谢!
EDIT: Okay since you guys say you would need to see the code here it is.
编辑:好的,因为你们说你需要在这里查看代码。
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int stringtoint(string s_convertee)
{
int i=0;
istringstream sin(s_convertee);
sin >> i;
return i;
}
int main()
{
string s_testnum = "233456091289474545356";
int n_maxmult = 0;
for (int i = 0; i<s_testnum.length(); i++)
{
int n_product = 1;
for (int j = i; j<(i+4); j++)
{
string s_multiplier = s_testnum.substr(j, 1);
int n_multiplier = stringtoint(s_multiplier);
n_product *= n_multiplier;
}
if (n_product>n_maxmult)
{
n_maxmult = n_product;
}
}
return 0;
}
回答by tjm
From C++ Reference for substr,
If the position passed is past the end of the string, an out_of_range exception is thrown.
如果传递的位置超过字符串的末尾,则抛出 out_of_range 异常。
So my guess would be your calling substr
with a first parameter that's greater than the strings length.
所以我的猜测是你调用substr
的第一个参数大于字符串长度。
Since you've posted your code, you can see,
既然你已经发布了你的代码,你可以看到,
i
can be a maximum of s_testnum.length()-1
,
i
最大可以是s_testnum.length()-1
,
but
但
j
goes up to i+4-1
= s_testnum.length()+2
.
j
上升到i+4-1
= s_testnum.length()+2
。
You then call substr
with a first parameter of j
which as said can be longer than the string length. So there's the problem.
然后substr
,您使用第一个参数调用,如上所述,j
该参数可以长于字符串长度。所以问题来了。
回答by Alok Save
As other answers have already pointed out, in substr
正如其他答案已经指出的那样,在 substr
If the position passed is past the end of the string, an out_of_range exception is thrown.
如果传递的位置超过字符串的末尾,则抛出 out_of_range 异常。
In your code:
在您的代码中:
for (int j = i; j<(i+4); j++)
When i
is 1
less than s_testnum.length()
j
goes past s_testnum.length()
and when you do, s_testnum.substr(j, 1);
causes an out_of_rangeexception.
When i
is 1
less thans_testnum.length()
j
过去s_testnum.length()
,当你过去时,s_testnum.substr(j, 1);
会导致out_of_range异常。
回答by Sean
Please post your code in question. Chances are you did something like:
请发布您有问题的代码。你有可能做了类似的事情:
std::string s("foo");
s.substr(5, 1); // The length of the string is 3, 5 is out of bounds
回答by Kiril Kirov
You're calling substr
with invalid parameters - you're trying to get element of the string, that is not there - for example trying to take the 10th char, when the string has only 5.
您正在substr
使用无效参数进行调用- 您正在尝试获取字符串的元素,该元素不存在 - 例如,当字符串只有 5 个时,尝试获取第 10 个字符。
In your case, this is caused by substr
- you're trying to get a substring, that is too long for the pointed start position and it "goes out" of the real string.
在您的情况下,这是由substr
- 您试图获取一个子字符串,该子字符串对于指向的起始位置来说太长,并且它“超出”了实际字符串。
terminate called after throwing an instance of 'std::out_of_range'
^^^^ says, that you have an uncaught exception, that is out_of_range
^^^^ 说,你有一个未捕获的异常,那就是 out_of_range
what(): basic_string::substr Aborted
^^^^ This is the text of the exception - note substr
^^^^ 这是例外的正文 - 注意 substr
回答by Sriram
Most likely that the parameter to the substr
function you called on some string in your code went past the string length. Hence the std::out_of_range
exception. But it is difficult to say without looking at the code. Also, you can step through the code and debug it yourself using a debugger like gdb/ddd. Just make sure to compile your code with the -g flag on g++.
substr
您在代码中对某个字符串调用的函数的参数很可能超过了字符串长度。因此std::out_of_range
例外。但是不看代码就很难说。此外,您可以单步调试代码并使用 gdb/ddd 之类的调试器自行调试。只需确保在 g++ 上使用 -g 标志编译您的代码。
回答by James Kanze
You should be able to arrange for your debugger to break when an exception is thrown. Run the program under the debugger, set the appropriate breakpoint, and look at the stack walkback. (For the record, Ctrl+Alt+E should bring up a dialog box with the exception handling options in Visual Studios; the command catch throw
activates a breakpoint on an exception under gdb
.)
您应该能够安排您的调试器在抛出异常时中断。在调试器下运行程序,设置合适的断点,看栈回溯。(作为记录,Ctrl+Alt+E 应该会在 Visual Studios 中打开一个带有异常处理选项的对话框;该命令会catch throw
在gdb
.下的异常上激活一个断点。)