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
If you don't flush your output, your output is not guaranteed to be visible outside your program. The fact that it is not printing in your terminal is just a consequence of the default behavior in linux to do line buffering when the output is a tty. If you run your program on linux with its output piped to another thing, like
./your_program | cat
Then the default buffer will be MUCH larger, most likely it will be at least 4096 bytes. So nothing will get displayed until the big buffer gets full. but really, the behaviour is OS specific unless you flush std::cout yourself.
To flush std::cout, use :
std::cout << std::flush;
also, using
std::cout << std::endl;
is a shortcut to
std::cout << '\n' << std::flush;