Where to use std?

Hi all,

I have just started to learn c++ and have encountered a small problem. I have found the following two versions to do the same thing:
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
{
    cout << "It works!";
    return 0;
}

and
1
2
3
4
5
6
7
#include <iostream>

int main()
{
    std::cout << "This works too!";
    return 0;
}


I would always prefere the first version bescause it saves me time for typing and I would assume less mistakes are made. However which version is better or are there good arguments for both?

I could imagin that the second version needs more time to compile (if the namespace std is loaded again and again, if that is the case) but on the other hand could there be conflicts with the between std and other namespaces needed for a program?

Thanks for the help!

@ramandpsingh
I am not sure if your posted this to the right thread. I am not looking for a GUI for C++ programing but trying to understand the difference (especialy the consequence) in the two codes above. Which way is more efficient for bigger programs and what are the pros and cons in general?
Don't worry that guy posted that to about 10 different threads...

Personally, I would use the second one. I don't know if you understand namespaces yet, but basically 'std' is a 'namespace' so all the standard library functions can be grouped together in it.

Supposing then you defined a function for variable with the same name as one in the standard library, you wouldn't get a problem because the standard library one would be safely inside 'std'.

By writing 'using namespace' you basically undo the good work done by putting the standard library inside the 'std' namespace. However, for small pieces of code, it shouldn't matter to use 'using namespace'.

Ultimately, it's up to you, but be careful with using namespace as your projects get bigger and you start to use more include files from different libraries.

For more on namespaces: http://cplusplus.com/doc/tutorial/namespaces/ (although this is classed as advanced material so don't worry if it's over your head ;) )

Hope this helps.
Last edited on
benbehr wrote:
Which way is more efficient for bigger programs and what are the pros and cons in general?
The second one is always better. namespaces do not influence the size or speed of your program. They solely exists for you and for the compiler to know which function to use. There's even the possibility that the compiler uses silently another function than you expect. So safety beats laziness.

You may use using namespace (very) locally to improve readability though
Ignore ramandpsingh, is a spammer.

The compile time shouldn't change but if it changes the first version should be slower since the compiler looks for cout in the global scope first and then in the std scope, in the second version it looks directly in the std namespace.
If you are using only one library you won't find any problem with using namespace but if you include multiple libraries which define the same symbol on different namespaces you may get some ambiguities.
Some standard symbols ( like list, count, etc ) have very simple and common names.
Here is an example of how that would cause an ambiguity:
1
2
3
4
5
6
7
8
9
10
#include <algorithm>
#include <iostream>
using namespace std;

int count = 0;

int main()
{
    cout << count; // do you want int count or std::count ?
}


If you don't want to type std:: too much you can use a specific using:

1
2
3
4
5
6
7
8
9
10
#include <algorithm>
#include <iostream>
using std::cout;

int count = 0;

int main()
{
    cout << count; // global variable int count
}

Topic archived. No new replies allowed.