使用命名空间 std
我正在学校上一门编程课,我想在课外开始做一些 C++ 编程。我的学校使用 Microsoft Visual C++ 6.0(从 1998 年开始),因此它仍然使用
而不是
和 using namespace std
。当我开始工作时,我不知道如何以及何时使用 using namespace std
以及何时仅使用 std::cout<<"Hello World!"< <'\n';
(例如)以及它的限制和名称空间关键字的其他用途。特别是,如果我想用 iostream 和 iomanip 制作一个程序,我是否必须声明“使用命名空间 std”两次,或者是否有一些不同的东西我也必须使用,或者我可以做同样的事情我用iostream 做了吗?我尝试用谷歌搜索,但我什么也没明白。预先感谢您的帮助。
I am taking a programming class in school and I wanted to start doing some c++ programming out of class. My school using Microsoft Visual C++ 6.0 (which is from 1998) so it still uses <iostream.h>
rather than <iostream>
and using namespace std
. When I started working, I couldn't figure out how and when to use using namespace std
and when to just use things like std::cout<<"Hello World!"<<'\n';
(for example) as well as it's limits and other uses for the namespace keyword. In particular, if I want to make a program with iostream and iomanip, do I have to state "using namespace std" twice, or is there something different that I would have to use as well, or can I just do the same thing as I did with iostream? I tried googling it but I didn't really understand anything. Thanks in advance for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,有一些事情,但它是可以管理的。
首先,两者之间的区别:
和 using
只是一个范围问题。范围只是一种奇特的方式来说明编译器如何识别变量和函数的名称等。命名空间只不过是在该命名空间内的所有变量上添加额外的作用域层。当您输入
using namespace std
时,您将获取命名空间std
内的所有内容并将其移动到全局范围,以便您可以使用较短的cout< /code> 而不是更完全限定的
std::cout
。关于命名空间需要理解的一件事是它们跨文件延伸。
和
都使用命名空间std
。因此,如果您同时包含这两个文件,则using namespace std
的声明将对这两个文件进行操作,并且这两个文件中的所有符号都将移动到程序的全局作用域(或函数的作用域,如果您在函数中使用了它)。会有人告诉你“不要使用
using namespace std
!!!”,但他们很少告诉你原因。假设我有以下程序,我要做的就是定义两个整数并将它们打印出来:当我使用
using namespace std
时,我打开了命名冲突的大门。如果我(随机地)将一个变量命名为与标头中定义的变量相同,那么您的程序将崩溃,并且您将很难找出原因。我可以通过不使用
using namespace std
语句来编写与以前相同的程序(但让它工作):希望这已经澄清了一些事情。
Ok, handful of things there, but it is manageable.
First off, the difference between:
And using
Is simply a matter of scope. Scope is just a fancy way of saying how the compiler recognizes names of variables and functions, among other things. A namespace does nothing more than add an extra layer of scope onto all variables within that namespace. When you type
using namespace std
, you are taking everything inside of the namespacestd
and moving it to the global scope, so that you can use the shortercout
instead of the more fully-qualifiedstd::cout
.One thing to understand about namespaces is that they stretch across files. Both
<iostream>
and<iomanip>
use the namespacestd
. Therefore, if you include both, then the declaration ofusing namespace std
will operate on both files, and all symbols in both files will be moved to the global scope of your program (or a function's scope, if you used it inside a function).There are going to be people who tell you "don't use
using namespace std
!!!!", but they rarely tell you why. Lets say that I have the following program, where all I am trying to do is define two integers and print them out:When I use
using namespace std
, I am opening the door for naming collisions. If I (by random chance), have named a variable to be the same thing as what was defined in a header, then your program will break, and you will have a tough time figuring out why.I can write the same program as before (but get it to work) by not using the statement
using namespace std
:Hopefully this has clarified a few things.
如果您使用不带
.h
的标头名称,则其中声明/定义的内容将位于std
命名空间中。您只需在要导入内容的范围内使用using namespace std;
一次即可获取所有内容;多个using namespace std;
没有任何帮助。不过,我一般建议不要使用
using namespace std;
。例如,我更喜欢使用using std::cout;
来代替,以防止std
中的名称与我的发生冲突。例如:
可能会导致神秘的问题,因为
left
和right
存在于std
命名空间中(作为 IO 操纵器),如果您懒洋洋地说using namespace std;
。如果您打算实际使用 IO 操纵器而不是输出变量,您可能会有点失望。但无论如何,其意图并不明显。也许您只是忘记了您有名为left
和right
的整数。相反,如果你说
或者
一切都按预期进行。另外,您还可以看到实际使用的内容(在本例中,不包含
中的任何内容),因此可以更轻松地将包含内容精简为您需要的内容。If you use the header names without the
.h
, then the stuff declared/defined in it will be in thestd
namespace. You only have to useusing namespace std;
once in the scope where you want stuff imported in order to get everything; more than oneusing namespace std;
doesn't help anything.I'd recommend against
using namespace std;
in general, though. I prefer to say, for example,using std::cout;
instead, in order to keep names instd
from conflicting with mine.For example:
may cause mysterious issues, because
left
andright
exist in thestd
namespace (as IO manipulators), and they get imported if you lazily sayusing namespace std;
. If you meant to actually use the IO manipulators rather than output the variables, you may be a bit disappointed. But the intent isn't obvious either way. Maybe you just forgot you have ints namedleft
andright
.Instead, if you say
or
everything works as expected. Plus, you get to see what you're actually using (which, in this case, includes nothing from
<iomanip>
), so it's easier to keep your includes trimmed down to just what you need.这里是一个很好的链接,描述了命名空间及其工作原理。
两种方法都是正确的,即可以用“using”语句引入命名空间,也可以限定命名空间的所有成员。这是编码风格的问题。我更喜欢使用命名空间进行限定,因为它可以让读者清楚地知道函数/类是在哪个命名空间中定义的。
此外,如果包含多个文件,则不必两次引入名称空间。一条 using 语句就足够了。
Here is a good link that describes namespaces and how they work.
Both methods are correct, that is, you can either introduce a namespace with the "using" statement or you can qualify all the members of the namespace. Its a matter of coding style. I prefer qualifying with namespaces because it makes it clear to the reader in which namespace the function / class is defined.
Also, you do not have to introduce a namespace twice if you are including multiple files. One using statement is enough.
好问题,瑞安。
using namespace
的作用是将给定命名空间(范围)中的所有符号导入到使用它的范围中。例如,您可以执行以下操作:在上面的示例中,命名空间
A
中的所有符号在命名空间B
中变得可见,就像它们在此处声明一样。此导入仅影响给定的翻译单元。因此,例如,当您在实现文件(即
.cpp
)中执行using namespace std;
时,您基本上会从std
导入所有符号命名空间进入全局范围。您还可以导入某些符号而不是所有内容,例如:
您可以在全局范围、命名空间范围或函数范围中执行此操作,如下所示:
由程序员决定何时使用完全限定名称以及何时使用
使用
关键字。通常,将using
放入头文件中是一种非常糟糕的做法。专业的 C++ 程序员几乎从不这样做,除非这是解决某些问题所必需的,或者他们 100% 确定这不会扰乱使用该标头的人的类型解析。然而,在源文件内部(没有人包含源文件),只要不同命名空间中没有冲突的名称,就可以执行任何类型的 using 语句。这只是一个品味问题。例如,如果整个代码中使用了大量来自不同命名空间的符号,我希望至少有一些关于它们实际声明位置的提示。但每个人都熟悉 STL,因此
using namespace std;
绝对不会造成任何损害。还可能有一些很长的命名空间,在这些情况下命名空间别名会很方便。例如,有一个 Boost.Filesystem 库,它将其所有符号放在
boost::filesystem
命名空间中。使用该命名空间会太多,所以人们通常会这样做:另外,在标头中使用命名空间别名几乎是可以的,如下所示:
.. 并受益于更少的打字。发生的情况是,将使用此标头的用户不会通过说
using namespace MyLib;
来导入整个文件系统库。但是,他们将从您的库中导入“fs”名称空间,这可能会与其他内容发生冲突。所以最好不要这样做,但如果你太想要它,最好比在那里说using namespace boost::filesystem
。回到你的问题。如果您使用 C++ I/O 流编写库,最好不要在标头中包含任何
using
语句,并且我会在每个中使用using namespace std;
cpp
文件。例如:somefile.hpp:
somefile.cpp:
Good question, Ryan. What
using namespace
does is importing all symbols from a given namespace (scope) into the scope where it was used. For example, you can do the following:In the above examples, all symbols in namespace
A
become visible in namespaceB
, like they were declared there.This import has affect only for a given translation unit. So, for example, when in your implementation file (i.e.
.cpp
) you dousing namespace std;
, you basically import all symbols fromstd
namespace into a global scope.You can also import certain symbols rather than everything, for example:
You can do that in global scope, namespace scope or function scope, like this:
It is up to a programmer to decide when to use fully qualified names and when to use
using
keyword. Usually, it is a very bad taste to putusing
into a header files. Professional C++ programmers almost never do that, unless that is necessary to work around some issue or they are 100% sure it will not mess up type resolution for whoever use that header.Inside the source file, however (nobody includes source files), it is OK to do any sort of using statements as long as there are no conflicting names in different namespaces. It is only a matter of taste. For example, if there are tons of symbols from different namespaces being used all over the code, I'd prefer at least some hints as for where they are actully declared. But everyone is familiar with STL, so
using namespace std;
should never do any harm.There also could be some long namespaces, and namespace aliasing comes handy in those cases. For example, there is a Boost.Filesystem library that puts all of its symbols in
boost::filesystem
namespace. Using that namespace would be too much, so people usually do something like this:Also, it is almost OK to use namespace aliasing in headers, like this:
.. and benefit from less typing. What happens is that users that will use this header, will not import the whole filesystem library by saying
using namespace MyLib;
. But then, they will import "fs" namespace from your library that could conflict with something else. So it is better not to do it, but if you want it too badly, it is better than sayingusing namespace boost::filesystem
there.So getting back to your question. If you write a library using C++ I/O streams, it is better not to have any
using
statements in headers, and I'd go withusing namespace std;
in everycpp
file. For example:somefile.hpp:
somefile.cpp:
特定于
using namespace std
你真的不应该在头文件中使用它。通过这样做,您已将整个“std”导入到包含您的头文件的任何人或包含包含您的文件的文件的任何其他人的全局命名空间中。
在 .cpp 文件中使用它是个人喜好。我通常不会将整个 std 导入到全局命名空间中,但自己这样做似乎没有任何害处,可以节省一些打字时间。
Specific to
using namespace std
You really shouldn't ever use it in a header file. By doing so, you've imported the entire 'std' into the global namespace for anyone who includes your header file, or for anyone else that includes a file that includes your file.
Using it inside a .cpp file, that's personal preference. I typically do not import the entire std into the global namespace, but there doesn't appear to be any harm in doing it yourself, to save a bit of typing.