为什么有经验的编码人员使用 std:: 而不是使用命名空间 std;?

2022-01-14 00:00:00 namespaces c++ std using-directives

可能重复:
为什么'使用命名空间std;'在 C++ 中被认为是一种不好的做法?

前几天我问了一个问题,有人回答说如果有人问问题,请向他们展示正确的方法,而不是 using namespace std; 我认为这有点奇怪,因为using namespace std; 更容易方式,但我想我现在失败了,因为我是一个初学者"编码器,你们知道得更好.

The other day when I asked a question someone replied saying if someone asks a question, show them the right way to do it instead of using namespace std; which I thought was a bit weird, as using namespace std; is way easier, But I guess I'm failing right now as I am a 'beginner' coder and you guys know better.

所以我想我的问题是:为什么使用 std:: 而不是 using namespace std;?

So I guess my question is: Why std:: instead of using namespace std;?

谢谢.

推荐答案

来自 C++ 常见问题:

From C++ FAQ:

我应该在我的代码中使用 using namespace std 吗?

Should I use using namespace std in my code?

可能不会.

人们不喜欢在上面输入 std::一遍又一遍,他们发现using namespace std 让编译器看到任何 std 名称,即使不合格.这美中不足的是它让编译器看到 any std 名称,甚至那些你没有想到的.在换句话说,它可以创建名称冲突和歧义.

People don't like typing std:: over and over, and they discover that using namespace std lets the compiler see any std name, even if unqualified. The fly in that ointment is that it lets the compiler see any std name, even the ones you didn't think about. In other words, it can create name conflicts and ambiguities.

https://isocpp.org/wiki/faq/coding-标准#using-namespace-std

相关文章