kto*_*yev 0 c++ string infinite-loop while-loop endl
我遇到了一些非常奇怪的事情.我遇到问题的代码是:
int stringPos;
int found1;
while (stringPos < 1);
{
//start searching inString for framen starting at foundn and record
found1 = inString.find(frame1, found1);
cout << found1 << endl;
//if return is a number, push back foundn to a vector
if (found1 != -1)
{
foundPositions.push_back(found1);
}
//if return is npos, then break the loop
else
{
stringPos=1;
}
//add 1 to foundn so that the search would continue from where the
//search ended last
found1+=1;
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,当我放在线cout << found1 << endl;下面时found1 = inString.find(frame1, found1);,循环正确执行.但是,如果我没有cout << found1 << endl;它进入无限循环......
有什么建议?谢谢!
这是一个错误(并使用一个整数变量):
while (stringPos < 1);
Run Code Online (Sandbox Code Playgroud)
因为它相当于:
while (stringPos < 1) {}
Run Code Online (Sandbox Code Playgroud)
如果没有进入无限循环,则跟随它的代码将仅执行一次.纠正:
stringPos和found1.size_t为stringPos和found作为std::string::find()不返回int,但回报率size_type(一般size_t).std::string::npos而不是-1测试未找到.| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |