![](/img/trans.png)
[英]c++ terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr:
[英]C++ string std::out_of_range: basic_string::substr
我正在編寫一個具有檢查數組中字符串相鄰字符的函數的程序。 我使用substring函數(除其他外)執行此操作,但是我需要一種使用if語句來避免空字符串的方法,例如在字符串中查找第一個字符的結尾字符。 我是剛開始使用字符串而不是char[]
。 我想要一些基本上可以做到這一點的東西:
if (!Empty ( strings[1].substr(0-1,0) )) {
std::cout<<"char to left of substr(0)="<<strings[1].substr(0-1,0);
}
當我嘗試引用不存在的字符時,出現運行時錯誤:
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range:
basic_string::substr
Abort trap: 6
哦,一個從索引-1開始的子串,長度為0。
或者,由於第一個參數是無符號的,所以它從std::numeric_limits<size_t>::max()
。
無論如何。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.