C++入門書に突然、

#include <iostream>
using namespace std;

int main(){
cout << "Hello, World!" << endl ;


や、

 #include <iostream>
using namespace std;

int main(){
cout << "Hello, World!" << flush ;


という様に、「endl」や「flush」が使われ、 困惑したので、違いを調べてみた。

結果から言うと、
「endl」=改行した上で、バッファーをフラッシュする
「flush」=改行はせず、バッファーをフラッシュする
であった。


疑問解決には、以下のサイトが参考になった。

C++ - endl and flushing the buffer (「stackoverflow」より)
http://stackoverflow.com/questions/4751972/c-endl-and-flushing-the-buffer

C++: “std::endl” vs “\n”(「stackoverflow」より)
http://stackoverflow.com/questions/213907/c-stdendl-vs-n

endl, ends, flushの正体 (「yohhoyの日記」より)
http://d.hatena.ne.jp/yohhoy/20120124/p1


また、「バッファーをフラッシュする」の意味が分からない場合は、以下が参考になる。

バッファについて (「つくる人の味方」より)
http://www.curiocube.com/mikata/hello/ch18_buffer.php


上記1番めのサイトの質問とベストアンサーを訳す。

C++ - endl and flushing the buffer (「stackoverflow」より)
http://stackoverflow.com/questions/4751972/c-endl-and-flushing-the-buffer
 
Q.
In the C++ primer book, in chapter (1), it mentions the following:

endl is a special value, called a manipulator, that when written to an output stream has the effect of writing a newline to the output and flushing the buffer associated with that device. By flushing the buffer, we ensure that the user will see the output written to the stream immediately.

What is meant by "flushing the buffer" here?

Thanks.
質問
C++の入門者の本の第1章に、次のように書いてありました。
 
endlは特殊な変数で、マニピュレーターと呼ばれます。書くと、改行の効果を持ち、また、デバイスに関連したバッファーをフラッシュします。バッファーをフラッシュすることにより、ユーザーは表示される出力をすぐに見ることができます。 

ここの「バッファーをフラッシュする」とは 何を意味するのでしょうか?

回答、ありがとうございます(*前もって感謝の意を述べる英語的表現です)
A.

Output is generally buffered before it's written to the intended device. That way, when writing to slow to access devices(like files), it doesn't have to access the device after every single character.

Flushing means emptying the buffer and actually writing it to the device.

回答

アウトプットは概ね出力先の機器に書かれる前にバッファーします(間があります)。そうすると、(ファイルのように)デバイスへの出力が遅れる時は、一文字ごとにデバイスにアクセスせずに済むのです。

フラッシュは、バッファーをなくし、すぐにデバイスに出力します。 



2番めの質問と回答を訳す

C++: “std::endl” vs “\n”(「stackoverflow」より)
http://stackoverflow.com/questions/213907/c-stdendl-vs-n
 

Q.

Many C++ books contain example code like this...

std::cout << "Test line" << std::endl;

...so I've always done that too. But I've seen a lot of code from working developers like this instead:

std::cout << "Test line\n";

Is there a technical reason to prefer one over the other, or is it just a matter of coding style?

質問

多くのC++の本は次のようなコードの例を挙げています。

std::cout << "Test line" << std::endl;

なので私もいつもそのようにしてきました。しかし、私は、これに代わる、実務に従事しているディベロッパーの書いたコードをたくさん見てきました。 

std::cout << "Test line\n";

最初の例と違い2番めの方が好ましい技術的な理由があるのでしょうか?それとも、コーディングのスタイルの違いでしょうか?

A.

The varying line-ending characters don't matter, assuming the file is open in text mode, which is what you get unless you ask for binary. The compiled program will write out the correct thing for the system compiled for.

The only difference is that std::endl flushes the output buffer, and '\n' doesn't. If you don't want the buffer flushed frequently, use '\n'. If you do (for example, if you want to get all the output, and the program is unstable), use std::endl. 

回答

バイナリを扱うことなく、テキストモードでファイルを開くことを想定しているなら、行末の文字(endlと\n)の種類は問題ありません。コンパイルされたプログラムはコンパイル先のシステムに合わせて正しく書きだされるでしょう。

唯一の違いは、 「std::endl」は出力のバッファーをフラッシュしますが、「'\n'」はしないということです。もしあなたがバッファーをフラッシュしたくないなら、「\n」を使うべきです。もしあなたが、(例えば、アウトプット全体を受け取りたく、(そうしないと)プログラムが不安定なら)「std::endl」を使うべきです。 


3番めのサイトによれば、他に、「ends」もあるのだとか。

endl, ends, flushの正体 (「yohhoyの日記」より)
http://d.hatena.ne.jp/yohhoy/20120124/p1
識別子概要効果
endl改行文字の出力後にフラッシュ処理os.put(os.widen('\n')); os.flush();*2
ends終端文字を出力os.put(charT());*3
flushフラッシュ処理os.flush();
 「ends」は、要するに、NULL文字の挿入。配列の最後に「\0(/0)」が入ることがありますが、その意味ですね。
詳しくは以下のサイトをどうぞ。

std::ends (「cplusplus.com」より)
http://www.cplusplus.com/reference/ostream/ends/