Strange behaviour of std::cout in Linux

前端 未结 2 2032
小鲜肉
小鲜肉 2021-01-23 09:11

I am trying to print results in 2 nested for cycles using std::cout. However, the results are not printed to the console immediately, but with delay (a

2条回答
  •  庸人自扰
    2021-01-23 09:34

    std::cout is an stream, and it is buffered. You can flush it by several ways:

    std::cout.flush();
    
    std::cout << std::flush;
    
    std::cout << std::endl;  // same as:  std::cout << "\n" << std::flush`
    


    johny:

    I am flushing the buffer before the cycle using std::endl. The problem arises when printing dot representing % of processed data inside the cycle.

    If you flush the buffer before the cycle, that does not affect the output in the cycle. You have to flush in or after the cycle, to see the output.

提交回复
热议问题