C++ 中的 include 和 using 命名空间
为了使用cout
,我需要指定:
#include<iostream>
和
using namespace std;
cout
在哪里定义?在 iostream 中,对吗?那么,iostream
本身就存在于命名空间 std
中吗? 这
两个语句对于使用 cout
的含义是什么? >?
我很困惑为什么我们需要将它们都包括在内。
for using cout
, I need to specify both:
#include<iostream>
and
using namespace std;
Where is cout
defined? in iostream
, correct? So, it is that iostream
itself is there in namespace std
?
What is the meaning of both the statements with respect to using cout
?
I am confused why we need to include them both.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
iostream
是定义 cout 的文件的名称。另一方面,std
是一个命名空间,相当于(在某种意义上)Java 的包。cout 是在 std 命名空间内的 iostream 文件中定义的实例。
另一个命名空间中可能存在另一个
cout
实例。因此,为了表明您想要使用std
命名空间中的cout
实例,您应该编写std::cout
来指示范围。为了避免到处出现
std::
,您可以使用using
子句。它们是两个不同的东西。一个表示范围,另一个实际包含
cout
。响应您的评论
想象一下,在 iostream 中存在两个名为
cout
的实例,位于不同的命名空间中:包含
后,您仍然需要指定命名空间。#include
语句并没有说“嘿,使用 std:: 中的 cout”。这就是using
的用途,指定范围。iostream
is the name of the file where cout is defined. On the other hand,std
is a namespace, equivalent (in some sense) to Java's package.cout is an instance defined in the
iostream
file, inside the std namespace.There could exist another
cout
instance, in another namespace. So to indicate that you want to use thecout
instance from thestd
namespace, you should writestd::cout
, indicating the scope.To avoid the
std::
everywhere, you can use theusing
clause.They are two different things. One indicates scope, the other does the actual inclusion of
cout
.In response to your comment
Imagine that in iostream two instances named
cout
exist, in different namespaces:After including
<iostream>
, you'd still need to specify the namespace. The#include
statement doesn't say "Hey, use the cout in std::". That's whatusing
is for, to specify the scope.如果您的 C++ 实现使用 C 样式头文件(很多都这样做),则有一个文件包含类似以下内容:
std 是 C++ 标准规定大多数标准内容应驻留在其中的命名空间。这是为了防止过度填充全局命名空间,这可能会导致您难以为自己的类、变量和函数命名,而这些名称尚未用作标准事物的名称。
通过说,
您告诉编译器,您希望它在查找名称时除了全局命名空间之外,还希望在命名空间 std 中进行搜索。
如果编译器看到源代码行:
在
using namespace std;
行之后的某处,它将在各种不同的命名空间(类似于范围)中查找foo
,直到找到一个 foo满足该行的要求。它按一定顺序搜索名称空间。首先,它查看本地作用域(实际上是一个未命名的命名空间),然后查找下一个最本地的作用域,直到一遍又一遍,直到函数之外,然后查找封闭对象的命名事物(在本例中为方法),然后查找全局名称(函数,在这种情况下,除非你很愚蠢并且我忽略了重载()),然后在 std 命名空间(如果你使用了using namespace std;
行)。我可能会将最后两个的顺序错误(std 可能会在全局之前搜索),但您应该避免编写依赖于此的代码。If your C++ implementation uses C style header files (many do) then there is a file that contains something similar to:
std is the namespace that the C++ standard says most of the standard things should reside in. This is to keep from overpopulating the global namespace, which could cause you difficulty in coming up with names for your own classes, variables, and functions which aren't already used as names for standard things.
By saying
you are telling the compiler that you want it to search in the namespace std in addition to the global namespace when looking up names.
If the compiler sees the source line:
somewhere after the
using namespace std;
line it will look forfoo
in various different namespaces (similar to scopes) until it finds a foo that meets the requirements of that line. It searches namespaces in a certain order. First it looks in the local scope (which is really an unnamed namespace), then the next most local scope until over and over until outside of a function, then at the enclosing object's named things (methods, in this case), and then at global names (functions, in this case unless you've been silly and overloaded () which I'm ignoring), and then at the std namespace if you've used theusing namespace std;
line. I may have the last two in the wrong order (std may be searched before global), but you should avoid writing code that depends on that.cout
在 iostream 中逻辑定义。从逻辑上讲,我的意思是它实际上可能位于文件 iostream 中,或者可能位于 iostream 包含的某个文件中。无论哪种方式,包含 iostream 都是获取cout
定义的正确方法。iostream 中的所有符号都位于命名空间
std
中。要使用cout
符号,您必须告诉编译器如何找到它(即什么命名空间)。您有几个选择:cout
is logically defined within iostream. By logically, I mean it may actually be in the file iostream or it may be in some file included from iostream. Either way, including iostream is the correct way to get the definition ofcout
.All symbols in iostream are in the namespace
std
. To make use the thecout
symbol, you must tell the compiler how to find it (i.e. what namespace). You have a couple of choices:#include
引用定义 cout 的头文件。如果您要使用 cout,那么您将始终需要 include。您不需要
using namespace std;
。这只是允许您使用简写cout
和endl
等,而不是std::cout
和std::endl
其中命名空间是显式的。就我个人而言,我不喜欢使用using namespace ...
因为它要求我明确表达我的意思,尽管它确实更冗长。The
#include <iostream>
references the header file that defines cout. If you're going to use cout, then you will always need the include.You do not need to
using namespace std;
. That's simply allows you to use the shorthandcout
andendl
and the like, rather thanstd::cout
andstd::endl
where the namespace is explicit. Personally I prefer not to useusing namespace ...
since it requires me to be explicit in my meaning, though it is admittedly more verbose.