XCode 4 only displaying cout statements with endl

谁说我不能喝 提交于 2019-12-11 06:17:59

问题


I have a really weird issue with my cout statements. I've only tried this out with XCode 4. For instance, if I write,

cout << "this works" << endl;
cout << "this doesnt";
cout << memorySizeLimit << " blocks of memory available." << endl;

I see all three output statements in my debugger console. However, if I change the order to,

cout << memorySizeLimit << " blocks of memory available." << endl;
cout << "this works" << endl;
cout << "this doesn't";

I only see the first two couts. Even stranger, if I change the code to,

cout << memorySizeLimit << " blocks of memory available." << endl;
cout << "this works" << endl;
cout << "this doesn't" << endl;

I see all three statements.

Why would I not see that "this doesn't" cout statement when I change its position?


回答1:


std::cout is an stream and normally it is buffered for performance. If you print endl the stream gets flushed (cout << endl is the same as cout << "\n" << flush.

You may flush the stream manually by cout << flush (or cout.flush()).

So that should print:

cout << "this doesn't" << flush;


来源:https://stackoverflow.com/questions/5453071/xcode-4-only-displaying-cout-statements-with-endl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!