std::unitbuf, std::nounitbuf
From cppreference.com
| ヘッダ <ios>で定義 |
||
| std::ios_base& unitbuf( std::ios_base& str ); |
(1) | |
| std::ios_base& nounitbuf( std::ios_base& str ); |
(2) | |
出力ストリームでの出力操作の後に自動フラッシュを有効または無効にします。入力には影響しません。
これはI/Oマニピュレータであり、std::basic_ostream 型の `out` に対して out << std::unitbuf のような式で呼び出すか、std::basic_istream 型の `in` に対して in >> std::unitbuf のような式で呼び出すことができます。
目次 |
[編集] 注記
フラッシュは、std::basic_ostream::sentry オブジェクトのデストラクタで実行されます。このデストラクタは、str.flags() & std::ios_base::unitbuf が true の場合、str.rdbuf()->pubsync() を呼び出します。
標準出力オブジェクト std::cerr および std::wcerr は、デフォルトで unitbuf ビットが設定されています。
[編集] パラメータ
| str | - | I/Oストリームへの参照 |
[編集] 戻り値
str (操作後のストリームへの参照)。
[編集] 例
std::unitbuf または他の明示的なフラッシュがない場合、出力は同じですが、リアルタイムでは表示されません。
このコードを実行
#include <chrono> #include <iostream> template<typename Diff> void log_progress(Diff d) { std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(d) << " ... "; } int main() { volatile int sink = 0; std::cout << std::unitbuf; // enable automatic flushing const auto start = std::chrono::high_resolution_clock::now(); for (int j = 0; j < 5; ++j) { for (int n = 0; n < 10000; ++n) for (int m = 0; m < 20000; ++m) sink += m * n; // do some work log_progress(std::chrono::high_resolution_clock::now() - start); } std::cout << '\n'; }
出力
571ms ... 1146ms ... 1722ms ... 2294ms ... 2865ms ...
[編集] 関連項目
| 出力ストリームをフラッシュする (関数テンプレート) | |
| '\n'を出力し、出力ストリームをフラッシュする (関数テンプレート) |