C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- 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::cerr, std::wcerr
std::cerr, std::wcerr
| å®ä¹äºå¤´æä»¶ <iostream>
|
||
| extern std::ostream cerr; |
(1) | |
| extern std::wostream wcerr; |
(2) | |
å
¨å±å¯¹è±¡ std::cerr å std::wcerr æ§å¶å°å®ç°å®ä¹ç±»åï¼åå«å¯¼åºèª std::streambuf å std::wstreambuf ï¼çæµç¼å²ï¼ä¸æ å C é误è¾åºæµ stderr å
³èã
ä¿è¯è¿äºå¯¹è±¡å¨æé é¦ä¸ª std::ios_base::Init ç±»å对象ä¹åææé´å¾å°åå§åï¼èä¸å¯ç¨äºå¸¦æåºåå§åçéæå¯¹è±¡çæé 彿°åææå½æ°ï¼åªè¦å¨å®ä¹å¯¹è±¡åå
å« <iostream> ï¼ã
é¤éåå¸ sync_with_stdio(false) ï¼ä»å¤ä¸ªçº¿ç¨ä¸ºææ ¼å¼åæ æ ¼å¼è¾åºè®¿é®è¿äºå¯¹è±¡æ¯å®å ¨çã
䏿¦åå§åï¼å (std::cerr.flags() & unitbuf) != 0 ï¼ wcerr ä¹ä¸æ ·ï¼ï¼è¿è¡¨ç¤ºä»»ä½åéç»è¿äºæµå¯¹è±¡çè¾åºé½è¢«ç«å³å²å
¥å° OS ï¼éè¿ std::basic_ostream::sentry çææå½æ°ï¼ã
å¦å¤ï¼ std::cerr.tie() è¿å &std::cout ï¼å¯¹ wcerr å wcout ç¸åï¼ï¼è¿è¡¨ç¤ºä»»ä½ std::cerr ä¸çè¾åºé¦å
æ§è¡ std::cout.flush() ï¼éè¿ std::basic_ostream::sentry çæé 彿°ï¼ã(C++11 èµ·)
注æ
åç§°ä¸ç 'c' æä»£âå符âï¼ stroustrup.com FAQ ï¼ï¼ cerr 表示âå符éè¯¯ï¼æµï¼âè wcerr 表示â宽å符éè¯¯ï¼æµï¼âã
示ä¾
éè¿ cerr è¾åºå° stderr ï¼å²å ¥ cout ä¸çå¾ å¤çè¾åºï¼èéè¿ clog è¾åºå° stderr ä¸ä¼ã
#include <thread> #include <iostream> #include <chrono> void f() { std::cout << "Output from thread..."; std::this_thread::sleep_for(std::chrono::seconds(2)); std::cout << "...thread calls flush()" << std::endl; } Â int main() { std::thread t1(f); std::this_thread::sleep_for(std::chrono::seconds(1)); std::clog << "This output from main is not tie()'d to cout\n"; std::cerr << "This output is tie()'d to cout\n"; t1.join(); }
è¾åºï¼
This output from main is not tie()'d to cout Output from thread...This output is tie()'d to cout ...thread calls flush()
åé
| åå§åæ åæµå¯¹è±¡ ( std::ios_base çå
¬å¼æåç±») | |
| åå
¥å°æ å C éè¯¯æµ stderr (å ¨å±å¯¹è±¡) | |
| åå
¥å°æ å C è¾åºæµ stdout (å ¨å±å¯¹è±¡) |