C++ 为什么此错误"basic_string::substr"并破坏循环
c++ why this error "basic_string::substr" and breaking up the loop
我试图得到一个词的最后一个字母,比较如果这些字母行是在一个向量。我想先检查最后2个字母,然后是最后3个和最后4个字母。一旦它找到一个,它就会中断,并返回false。否则,它应该检查所有留下的东西,如果没有发现,就返回,这是真的。
This my function:
bool isIt(wstring word, vector <wstring> vec) {
int ct = 2;
while (ct < 5) {
word = word.substr(word.length() - ct, word.length()-1);
//wcout << word << endl;
if (find(vec.begin(), vec.end(), word) != vec.end()) {
//wcout << "false" << endl;
ct = 5; return false;
} else {ct++; wcout << ct << endl; continue; }
} return true;}
函数通过this调用:
if( word >3){ isIt(word, vec); }
当第一次检查失败时,我收到这个错误消息:
抛出std::out_of_range实例后调用终止(): basic_string: substr
我不明白,为什么它不继续,一旦它在else部分。我希望我的描述足够好。BR
bug在这里,在这里修改word
。
word = word.substr(word.length() - ct, word.length()-1);
如果word
是"ABCD"
进入这个函数,那么第一次通过循环,结果为:
word = std::string("ABCD").substr( 2, 3 );
第二次循环的结果是:
word = std::string("CD").substr( size_t(-1), 1 );
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 循环中的随机函数
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- C++ 为什么此错误"basic_string::substr"并破坏循环