什么需要我声明“using namespace std;"? [英] What requires me to declare "using namespace std;"?

查看:32
本文介绍了什么需要我声明“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;

这篇关于什么需要我声明“using namespace std;"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆