![](/img/trans.png)
[英]Why does a cout statement at the beginning of my program not output anything?
[英]Why does my cout output not appear immediately?
它不會打印放入循環中的字符串。 該程序是在 g++ 的幫助下編寫的,包含 sys/types.h 頭文件
for(int i=0;i<9;i++)
{
cout<<"||";
sleep(1);
}
你沒有刷新你的輸出。
std::cout << "||" << std::flush;
您可能在這里看到的是輸出被緩沖的效果。 通常,在使用std::endl
之前,實際上不會寫入輸出。
for(int i=0;i<9;i++)
{
// Flushes and adds a newline
cout<< "||" << endl;
sleep(1);
}
在引擎蓋下std::endl
添加一個換行符,然后使用std::flush
強制輸出到控制台。 可以直接使用std::flush
來達到同樣的效果
for(int i=0;i<9;i++)
{
cout << "||" << flush;
sleep(1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.