[英]C++ string std::out_of_range: basic_string::substr
I am making a program with a function that checks for the adjacent characters of a string in an array. 我正在编写一个具有检查数组中字符串相邻字符的函数的程序。 I do this with the substring function (among other things), but I need a way to use an if statement to avoid empty strings, like finding the trailing character of the first character in a string.
我使用substring函数(除其他外)执行此操作,但是我需要一种使用if语句来避免空字符串的方法,例如在字符串中查找第一个字符的结尾字符。 I am new to using strings instead of
char[]
. 我是刚开始使用字符串而不是
char[]
。 I want some that essentially does this: 我想要一些基本上可以做到这一点的东西:
if (!Empty ( strings[1].substr(0-1,0) )) {
std::cout<<"char to left of substr(0)="<<strings[1].substr(0-1,0);
}
When I try to reference a non existent char, I get the runtime error: 当我尝试引用不存在的字符时,出现运行时错误:
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range:
basic_string::substr
Abort trap: 6
Oh, a substring starting at index -1, of length 0. 哦,一个从索引-1开始的子串,长度为0。
Or, since the first argument is unsigned, it starts at std::numeric_limits<size_t>::max()
. 或者,由于第一个参数是无符号的,所以它从
std::numeric_limits<size_t>::max()
。
Anyway. 无论如何。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.