[英]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.