简体   繁体   English

C ++字符串std :: out_of_range:basic_string :: substr

[英]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.

相关问题 c++ 在抛出&#39;std::out_of_range&#39; what() 实例后调用终止:basic_string::substr: - c++ terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: &#39;std :: out_of_range&#39;what():basic_string :: substr:__ post - 'std::out_of_range' what(): basic_string::substr: __pos 问题 - 在抛出&#39;std::out_of_range&#39; what() 实例后调用 C++ 终止:basic_string::substr:? - Question - c++ terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr:? C++:终止调用 &#39;std::out_of_range&#39; what(): basic_string::substr: __pos (which is 1) &gt; this-&gt;size() (which is 0) - C++: terminate called 'std::out_of_range' what(): basic_string::substr: __pos (which is 1) > this->size() (which is 0) c++ 在抛出实例后调用终止 'std::out_of_range' what(): basic_string::substr: __pos &gt; this-&gt;size() - c++ terminate called after throwing an instance 'std::out_of_range' what(): basic_string::substr: __pos > this->size() 抛出&#39;std :: out_of_range&#39;what():basic_string :: insert实例后,c ++终止调用 - c++ terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::insert 抛出&#39;std :: out_of_range&#39;what():basic_string :: substr的实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr 字符串c ++:libc ++ abi.dylib:以类型为std :: out_of_range的未捕获异常终止:basic_string - string c++ : libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string 在抛出&#39;std::out_of_range&#39; what() 实例后调用 C++ 终止:basic_string::replace: __pos - C++ terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::replace: __pos 抛出&#39;std :: out_of_range&#39;的实例之后调用终止终止what():basic_string :: substr:__pos(为1)&gt; this-&gt; size()(为0) - terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 1) > this->size() (which is 0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM