I understand that using namespace std;
is problematic (e.g. from reading the answers to "Why is using namespace std
considered bad practice?").
What are good alternatives to importing the standard namespace like that?
I'd like to know what I can do to improve my code.
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; }
- this does not pollute the global namespace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With