我真的很困惑这个简单的问题。 这里是我的代码:string.length()导致问题
#include <iostream>
#include <string>
int main()
{
std::string str = "bb";
int counter;
for (counter = str.length() - 1; counter >= str.length() - 2; counter--)
{
std::cout << "counter: " << counter << std::endl;
}
}
基本上,它应该只打印两行,然后终止该程序,但它使印刷生产线。事实上,循环不会在counter = -1停止,这很奇怪!为什么会发生?
在C++的全局命名空间中使用'void main()'是非法的。你应该使用'int main(void)'来代替。 – MikeCAT
或'int main()'。 – TartanLlama
@MikeCAT:你的意思是非法?!它工作正常。 – Diamond