std::unitbuf, std::nounitbuf
來自 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' 並重新整理輸出流 (函式模板) |