C++ 中的 include 和 using 命名空间

发布于 2024-08-29 01:19:28 字数 344 浏览 12 评论 0原文

为了使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂ガ小子 2024-09-05 01:19:28

iostream 是定义 cout 的文件的名称。另一方面,std 是一个命名空间,相当于(在某种意义上)J​​ava 的包。

cout 是在 std 命名空间内的 iostream 文件中定义的实例。

另一个命名空间中可能存在另一个 cout 实例。因此,为了表明您想要使用 std 命名空间中的 cout 实例,您应该编写

std::cout 来指示范围。

std::cout<<"Hello world"<<std::endl;

为了避免到处出现 std::,您可以使用 using 子句。

cout<<"Hello world"<<endl;

它们是两个不同的东西。一个表示范围,另一个实际包含 cout

响应您的评论

想象一下,在 iostream 中存在两个名为 cout 的实例,位于不同的命名空间中:

namespace std{
   ostream cout;
}
namespace other{
   float cout;//instance of another type.
}

包含 后,您仍然需要指定命名空间。 #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 the cout instance from the std namespace, you should write

std::cout, indicating the scope.

std::cout<<"Hello world"<<std::endl;

To avoid the std:: everywhere, you can use the using clause.

cout<<"Hello world"<<endl;

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:

namespace std{
   ostream cout;
}
namespace other{
   float cout;//instance of another type.
}

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 what using is for, to specify the scope.

jJeQQOZ5 2024-09-05 01:19:28

如果您的 C++ 实现使用 C 样式头文件(很多都这样做),则有一个文件包含类似以下内容:

#include ... // bunches of other things included

namespace std {

... // various things

extern istream cin;
extern ostream cout;
extern ostream cerr;

... // various other things

}

std 是 C++ 标准规定大多数标准内容应驻留在其中的命名空间。这是为了防止过度填充全局命名空间,这可能会导致您难以为自己的类、变量和函数命名,而这些名称尚未用作标准事物的名称。

通过说,

using namespace std;

您告诉编译器,您希望它在查找名称时除了全局命名空间之外,还希望在命名空间 std 中进行搜索。
如果编译器看到源代码行:

return foo();

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:

#include ... // bunches of other things included

namespace std {

... // various things

extern istream cin;
extern ostream cout;
extern ostream cerr;

... // various other things

}

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

using namespace std;

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:

return foo();

somewhere after the using namespace std; line it will look for foo 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 the using 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.

欲拥i 2024-09-05 01:19:28

cout 在 iostream 中逻辑定义。从逻辑上讲,我的意思是它实际上可能位于文件 iostream 中,或者可能位于 iostream 包含的某个文件中。无论哪种方式,包含 iostream 都是获取 cout 定义的正确方法。

iostream 中的所有符号都位于命名空间 std 中。要使用cout符号,您必须告诉编译器如何找到它(即什么命名空间)。您有几个选择:

// explicit
std::cout << std::endl;

// import one symbol into your namespace (other symbols must still be explicit)
using std::cout;
cout << std::endl;

// import the entire namespace
using namespace std;
cout << endl;

// shorten the namespace (not that interesting for standard, but can be useful
// for long namespace names)
namespace s = std;
s::cout << s::endl;

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 of cout.

All symbols in iostream are in the namespace std. To make use the the cout symbol, you must tell the compiler how to find it (i.e. what namespace). You have a couple of choices:

// explicit
std::cout << std::endl;

// import one symbol into your namespace (other symbols must still be explicit)
using std::cout;
cout << std::endl;

// import the entire namespace
using namespace std;
cout << endl;

// shorten the namespace (not that interesting for standard, but can be useful
// for long namespace names)
namespace s = std;
s::cout << s::endl;
纸伞微斜 2024-09-05 01:19:28

#include 引用定义 cout 的头文件。如果您要使用 cout,那么您将始终需要 include。

您不需要using namespace std;。这只是允许您使用简写 coutendl 等,而不是 std::coutstd::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 shorthand cout and endl and the like, rather than std::cout and std::endl where the namespace is explicit. Personally I prefer not to use using namespace ... since it requires me to be explicit in my meaning, though it is admittedly more verbose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文