【发布时间】:2010-08-02 04:03:20
【问题描述】:
这是非常简单的代码,
#include <iostream>
using namespace std;
int main() {
unsigned int u=10;
int i;
int count=0;
for (i=-1;i<=u;i++){
count++;
}
cout<<count<<"\n";
return 0;
}
count的值为0,为什么?
【问题讨论】:
-
顺便说一句,您应该使用
std::endl而不是将\n插入到流中,因为std::endl两者都是可移植的并确保输出立即显示。 -
@Ben:
\n一样便携;如果你现在特别想刷新流,你应该只使用endl。 -
\n是可移植的(它被转换为平台的换行序列),并且不承担立即刷新流的开销。你应该使用具有你想要的语义的那个:如果你想刷新流,使用endl,如果你只是想要一个换行符,使用\n。 -
std::endl 等价于 "\n"
标签: c++