Using std::endl flushes the output buffer after sending a '\n',
which means std::endl is more expensive in performance. Obviously if you
need to flush the buffer after sending a '\n', then use std::endl;
but if you don't need to flush the buffer, the code will run faster if you use
'\n'.
This code simply outputs a '\n':
void f()
{
std::cout << ...stuff... << '\n';
}
This code outputs a
'\n', then flushes the output buffer:
void g()
{
std::cout << ...stuff... << std::endl;
}
This code simply flushes the output buffer:
void h()
{
std::cout << ...stuff... << std::flush;
}
Note: all three of the above examples require
#include <iostream>