using namespace std; vs using std::x

Jul 11, 2011 at 2:43am
Hey, so I keep seeing people using the using namespace std; deal. I personally just do the using::std x structure. For example, I see programs started like:

#include <iostream>
using namespace std;

and my programs start like this:

#include <iostream>

using std::cout;
using std::cin;
etc....

Why is there a difference? I tried googling this already, I couldnt find much on it. Is one way better than the other? What exactly does using namespace std; do?

Thanks ahead of time! Look forward to some answers :D
Jul 11, 2011 at 2:50am
when you use using namespace std its like opening the garage door to let everyone see all your crap. it allows everything under that namespace to be used. when you do it the other way like st::cout you only allow 1 piece of it.
Jul 11, 2011 at 2:51am
Hmmm, im still a noob too, but i would think that NAMESPACE is a variable?
Jul 11, 2011 at 3:07am
namespace is not a variable. It's a keyword.
http://cplusplus.com/doc/tutorial/namespaces/
Jul 11, 2011 at 3:33am
Ok so unless you need to use everything in whatever library, use the std:: format? And does using namespace std; work for any library?
Jul 11, 2011 at 3:46am
And does using namespace std; work for any library?

No. But,
All the files in the Standard C++ Library are included in namespace 'std'.

Unless you are creating a very BIG program AND you run out of names for your variables, func......., use the using directive, Otherwise you will simply be wasting your time typing extra stuff.
Last edited on Jul 11, 2011 at 3:53am
Jul 11, 2011 at 4:12am
Unless you are creating a very BIG program AND you run out of names for your variables, func......., use the using directive, Otherwise you will simply be wasting your time typing extra stuff.


Ehhhh.

I can't say I agree.
Last edited on Jul 11, 2011 at 4:13am
Jul 11, 2011 at 4:43am
Let's just say that among other things it's very handy to be able to just look at your code and instantly know which library what's in, just by the namespace:: prefixes. ;)

-Albatross
Jul 17, 2011 at 6:15am
Sorry for not being active in here! I forgot to subscribe to this :/ So using the using directive or namespaces, which is better? Right now, I am still just working in console apps, hoping to start working on window apps soon.
Topic archived. No new replies allowed.