在哪里放使用namespace std; [英] where to put using namespace std;
问题描述
我想知道在中使用namespace std;
。我在 int main(){}
中看到了一个使用的命名空间std;
的代码, #include< iostream>
。
I'm wondering where to put using namespace std;
. I saw a code with the using namespace std;
in the int main(){}
but I was putting it after #include <iostream>
. Where should I put it and does it make any difference where I put it?
推荐答案
这会使巨大
如果你把它放在一个函数中,那么它只适用于该函数。
If you put it inside a function, then it only applies in that function.
如果将它放在全局范围内的函数之外,那么它会应用于该点之后的所有内容。
If you put it outside a function in global scope then it applies to everything after that point.
如果将它放在全局范围的函数之外
If you put it outside a function in global scope in a header file then it will even apply to ever file that includes that header.
一般来说,在标题中使用它在全局范围是非常糟糕的做法,而半不良实践在全局范围内使用它,因为在 Unity builds 中,标题和来源之间的区别文件是模糊的。
Generally, it's very bad practice to use it at global scope in a header, and semi bad practice to use it in global scope at all since in Unity builds, the distinction between headers and source files is blurred.
你最好只是在你需要的函数中使用它,或者甚至不使用它,只是前缀标准库函数/类与 std ::
。
You would be best to just use it in the functions that you need it, or even not use it at all and just prefix standard library functions/classes with std::
.
这篇关于在哪里放使用namespace std;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!