What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?
stdout and stderr are different streams, even though they both refer to console output by default. Redirecting (piping) one of them (e.g. program.exe >out.txt
) would not affect the other.
Generally, stdout should be used for actual program output, while all information and error messages should be printed to stderr, so that if the user redirects output to a file, information messages are still printed on the screen and not to the output file.
以前关于什么时候用cout, 什么时候用cerr有比较大的随意性质,总结一下:显式print 一定要用std::cout, 其余情况,用情况主要用std::cerrclass ClassA
{
void print()
{
std::cout<<....
}
};