C++核心準則?SL.io.3:優(yōu)先使用iostream進行I / O

SL.io.3: Prefer?iostreams for I/O
SL.io.3:優(yōu)先使用iostream進行I / O
Reason(原因)
iostreams are safe, flexible, and extensible.
iostream安全,靈活且可擴展。
Example(示例)
// write a complex number:
complex z{ 3, 4 };
cout << z << '\n';
complex?is a user-defined type and its I/O is defined without modifying the?iostream?library.
complex是用戶定義的類型,無需修改iostream庫即可定義其I / O。
Example(示例)
// read a file of complex numbers:
for (complex z; cin >> z; )
v.push_back(z);
Exception(例外)
??? performance ???
??? 性能???
Discussion:?iostreams vs. the?printf()?family
討論:iostreams與printf()函數(shù)群
It is often (and often correctly) pointed out that the?printf()?family has two advantages compared to?iostreams: flexibility of formatting and performance. This has to be weighed against?iostreams advantages of extensibility to handle user-defined types, resilient against security violations, implicit memory management, and?locale?handling.
人們經(jīng)常(經(jīng)常正確地)指出,與iostream相比,printf()函數(shù)群具有兩個優(yōu)點:格式化的靈活性和性能。必須權(quán)衡iostream的優(yōu)勢:處理用戶定義的類型時的可擴展性,可抵御安全性違規(guī),隱式內(nèi)存管理和區(qū)域設(shè)置處理。
If you need I/O performance, you can almost always do better than?printf().
如果需要保證I / O性能,iostream幾乎可以總是比printf()更好。
gets(),?scanf()?using?%s, and?printf()?using?%s?are security hazards (vulnerable to buffer overflow and generally error-prone). C11 defines some "optional extensions" that do extra checking of their arguments. If present in your C library,?gets_s(),?scanf_s(), and?printf_s()?might be safer alternatives, but they are still not type safe.
使用%s的gets(),scanf()和使用%s的printf()都有安全隱患(很容易造成緩沖區(qū)溢出并且通常容易出錯)。C11定義了一些“可選擴展”,對它們的參數(shù)進行了額外的檢查。如果是在使用C語言庫,gets_s(),scanf_s()和printf_s()可能是更安全的選項,但它們?nèi)匀徊皇穷愋桶踩摹?/p>
Enforcement(實施建議)
Optionally flag?
(可選)標(biāo)記
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#slio3-prefer-iostreams-for-io
新書介紹
《實戰(zhàn)Python設(shè)計模式》是作者最近出版的新書,拜托多多關(guān)注!

本書利用Python 的標(biāo)準GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設(shè)計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設(shè)計模式的利弊,并合理運用設(shè)計模式。
對設(shè)計模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計和開發(fā)的參考;使用Python 語言進行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請分享給更多人。
關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
