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

查看:126
本文介绍了什么要求我声明“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天全站免登陆