什么需要我声明“using namespace std;"?

这个问题可能是重复的,但我找不到一个好的答案.简简单单,需要我声明什么

This question may be a duplicate, but I can't find a good answer. Short and simple, what requires me to declare

using namespace std;

在 C++ 程序中?

推荐答案

自从 C++ 标准被接受以来,几乎所有的标准库都在 std 命名空间内.因此,如果您不想使用 std:: 限定所有标准库调用,则需要添加 using 指令.

Since the C++ standard has been accepted, practically all of the standard library is inside the std namespace. So if you don't want to qualify all standard library calls with std::, you need to add the using directive.

然而,

using namespace std;

被认为是一种不好的做法,因为您实际上是在导入整个标准命名空间,从而为名称冲突提供了很多可能性.最好只导入您在代码中实际使用的内容,例如

is considered a bad practice because you are practically importing the whole standard namespace, thus opening up a lot of possibilities for name clashes. It is better to import only the stuff you are actually using in your code, like

using std::string;

相关文章