使用命名空间std和库 [英] using namespace std and library
问题描述
为什么我们需要头文件"和 using名称空间
标记来使任何库函数正确执行.例如,除非我们使用 iostream
,否则 cout
将不起作用.除非我们使用"using namespace std",否则它将无法正常工作.我的问题是,为什么我们需要使用命名空间std 的以及
cout
的 #include< iostream>
的组合才能成功执行?
Why we need both the "header file" and the using namespace
tag for the any library function to get executed properly. For example cout
will not work unless we use iostream
. Also it will not work unless we use "using namespace std". My question is why do we need combination of both using namespace std
as well as #include <iostream>
for cout
to execute successfully?
推荐答案
包含库标头可使该库功能对您的程序代码可见.否则,您的程序将不知道该库甚至存在.这是必需的部分.
Including a library header makes the library feature visible to your program code. Without that, your program has no idea that the library even exists. This is the part that is necessary.
使用命名空间std 编写只是允许您编写
cout
,而不是全名,即 std :: cout
.这很方便,仅此而已.
Writing using namespace std
simply allows you to write cout
rather than the full name which is std::cout
. It's a convenience, that's all.
这篇关于使用命名空间std和库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!