C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- è¾å ¥/è¾åºæçºµç¬¦
- std::showpoint, std::noshowpoint
- std::setprecision
- std::fixed, std::scientific, std::hexfloat, std::defaultfloat
- std::setbase
- std::showbase, std::noshowbase
- std::quoted
- std::boolalpha, std::noboolalpha
- std::dec, std::hex, std::oct
- std::setfill
- std::setw
- std::left, std::right, std::internal
- std::showpos, std::noshowpos
- std::uppercase, std::nouppercase
- std::ws
- std::ends
- std::skipws, std::noskipws
- std::flush
- std::endl
- std::flush_emit
- std::unitbuf, std::nounitbuf
- std::emit_on_flush, std::no_emit_on_flush
- std::resetiosflags
- std::setiosflags
- std::get_money
- std::get_time
- std::put_money
- std::put_time
- C 飿 ¼æä»¶è¾å ¥/è¾åº
- std::basic_streambuf
- std::basic_filebuf
- std::basic_stringbuf
- std::strstreambuf
- std::basic_syncbuf
- std::basic_ios
- std::basic_istream
- std::ios_base
- std::basic_osyncstream
- std::basic_ostream
- std::basic_iostream
- std::basic_ifstream
- std::basic_ofstream
- std::basic_fstream
- std::basic_istringstream
- std::basic_ostringstream
- std::basic_stringstream
- std::istrstream
- std::ostrstream
- std::strstream
- std::streamoff
- std::streamsize
- std::fpos
- std::iostream_category
- std::io_errc
- std::cin, std::wcin
- std::cout, std::wcout
- std::cerr, std::wcerr
- std::clog, std::wclog
- 注é
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >è¾å ¥/è¾åºåº >è¾å ¥/è¾åºæçºµç¬¦ > std::unitbuf, std::nounitbuf
std::unitbuf, std::nounitbuf
| å®ä¹äºå¤´æä»¶ <ios>
|
||
| std::ios_base& unitbuf( std::ios_base& str ); |
(1) | |
| std::ios_base& nounitbuf( std::ios_base& str ); |
(2) | |
å¯ç¨æç¦ç¨ä»»ä½è¾åºæä½åçèªå¨å²å ¥ãå¨è¾å ¥æ¶æ ææã
1) å¦åç¨è°ç¨ str.setf(std::ios_base::unitbuf) å¯ç¨æµ str ä¸ç unitbuf æ å¿
2) å¦åç¨è°ç¨ str.unsetf(std::ios_base::unitbuf) ç¦ç¨æµ str ä¸ç unitbuf æ å¿
è¿æ¯ä¸ä¸ª I/O æçºµç¬¦ï¼å¯ç¨å¦ out << std::unitbuf ç表达å¼å¯¹ä»»ä½ std::basic_ostream ç±»åç out æç¨å¦ in >> std::unitbuf ç表达å¼å¯¹ä»»ä½ std::basic_istream ç±»åç in è°ç¨ã
注解
å¨ 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 <iostream> #include <chrono>  template<typename Diff> void log_progress(Diff d) { std::cout << "..(" << std::chrono::duration_cast<std::chrono::milliseconds>(d).count() << " ms).."; }  int main() { volatile int sink = 0; std::cout << std::unitbuf; // å¯ç¨èªå¨å²å ¥  auto t1 = 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; // åä¸äºå·¥ä½ auto now = std::chrono::high_resolution_clock::now(); log_progress(now - t1); } std::cout << '\n'; }
è¾åºï¼
..(450 ms)....(902 ms)....(1352 ms)....(1802 ms)....(2252 ms)..
åé
| 岿´è¾åºæµ (彿°æ¨¡æ¿) | |
| è¾åº '\n' 并岿´è¾åºæµ (彿°æ¨¡æ¿) |