What are good alternatives to "using namespace std;"?
This question already has an answer here:
The main alternatives to bringing in everything from the std
namespace into the global one with using namespace std;
at global scope are:
Only bring in the actual names you need. For example just bring in vector
with using std::vector;
Always use explicit namespace qualifications when you use a name. For example std::vector<int> v;
(in headers, this should almost always be the only thing you do)
Bring in all names, but in a reduced scope (like only inside a function). For example void f() { using namespace std; vector<int> v; }
void f() { using namespace std; vector<int> v; }
void f() { using namespace std; vector<int> v; }
- this does not pollute the global namespace.
The alternative is to write std::
everywhere. It's frowned upon because of name collisions and because it's unclear. If you just write vector
I don't immediately know if you're using some math 3d vector or the standard library vector or something else. If you write std::vector
it's clear. If you using namespace std
, vector
may collide with my own 3d math class called vector
.
上一篇: 为什么C ++ STL如此严重地基于模板? (而不是*接口*)
下一篇: “使用名称空间标准”有什么好的选择?