[英]Does cout/cin internally call printf() / scanf() like `new` calls malloc?
我已經通過互聯網查找了它。 我發現了很多比較cin
與scanf()
和cout
與printf()
的答案,但從未發現cin
是否實際上在內部使用scanf()
就像 C++ 中的new
運算符使用malloc()
function 的 Z0D617F8370CAD1D4DE112F
C++ 標准沒有指定標准庫設施如std::cin
和std::cout
是如何實現的,只指定它們的行為方式。 C++ I/O 函數是否調用其 C 對應函數取決於實現。
作為如何實現 C++ I/O 流的示例,我們可以查看 libstdc++ 的源代碼,它是 GCC 的標准庫實現。 The std::basic_istream& operator>>(int&)
function which is the one called when you use std::cin >> x
to read an int calls some functions which call other functions and it eventually reaches this _M_extract_int
function that actually parses the integer . 因此,libstdc++ 沒有為使用 C I/O 函數的整數實現 stream 提取運算符。 不過,請記住,這只是一個示例,其他標准庫實現可能會有所不同。
C++ 標准規定了對象std::cout
和std::cin
必須做什么。 如何實現取決於供應商。
最好的方法是閱讀給定實現的源代碼。
您還需要知道,在后台printf()
還使用其他功能。 用它們實現cout
是明智的,因為這個 object 與printf()
function 的工作方式不同。
使用scanf()
的std::cin
也幾乎沒有機會,因為它往往會出現問題(閱讀更多關於遠離scanf()
的初學者指南)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.