A quick note:
You'll usually want to add a newline on output so either
std::cout<<"Hi all!\n";
std::cout<<"Hi all!"<<'\n';
or
std::cout<<"Hi all!"<<std::endl;
and depending on your compiler/environment the return 0 is not always needed, but I recommend always adding it anyway, especially if you are developing cross-compiler/cross-platform.
You're welcome, also, a newline will usually (but not always) flush the buffer, printing all output to the console. Forcing the buffer to flush can be achieved with std::flush, but when you need to print a newline AND need to force the buffer to flush you have the 2-in-1 command (as shown above) std::endl which is equivalent to '\n' << std::flush.
ok, Awesome. Can you explain the concept of a buffer to me? I have read a little about it when dealing with streams in java but I still can't wrap my head around it.
Well, I won't get deep into it, but it's sort of just like a buffer for video when you watch anything that streams to you, it is the place that stores the data before it is displayed, with video it's video data (still just data as far as a machine is concerned), here we are speaking about string or character data (text, from cout, to cin etc...).
In video data if the content didn't come to you and be stored in the buffer first it would continuously stutter showing one or few frames at a time before it had to download the next set of frames. The buffer here is what is put into cout in the stream and unless you send a newline (in most cases, but not always automatically flushing) or flush the buffer, it may not display when you want it to. It really depends on a variety of things mostly depending on what you're doing.
There is more to it than this, of course.
So TLDR;
It is the place where the string or character data for a stream is stored before it is displayed; buffering the data.
A quick note:
You'll usually want to add a newline on output so either
or
and depending on your compiler/environment the
return 0
is not always needed, but I recommend always adding it anyway, especially if you are developing cross-compiler/cross-platform.Awesome, I had no idea. Thank you :)
You're welcome, also, a newline will usually (but not always) flush the buffer, printing all output to the console. Forcing the buffer to flush can be achieved with
std::flush
, but when you need to print a newline AND need to force the buffer to flush you have the 2-in-1 command (as shown above)std::endl
which is equivalent to'\n' << std::flush
.ok, Awesome. Can you explain the concept of a buffer to me? I have read a little about it when dealing with streams in java but I still can't wrap my head around it.
Well, I won't get deep into it, but it's sort of just like a buffer for video when you watch anything that streams to you, it is the place that stores the data before it is displayed, with video it's video data (still just data as far as a machine is concerned), here we are speaking about string or character data (text, from cout, to cin etc...).
In video data if the content didn't come to you and be stored in the buffer first it would continuously stutter showing one or few frames at a time before it had to download the next set of frames. The buffer here is what is put into
cout
in the stream and unless you send a newline (in most cases, but not always automatically flushing) or flush the buffer, it may not display when you want it to. It really depends on a variety of things mostly depending on what you're doing.There is more to it than this, of course.
So TLDR;
It is the place where the string or character data for a stream is stored before it is displayed; buffering the data.
Ok, that actually helps a lot. Thank you again. You have been a huge help!