Ok, handful of things there, but it is manageable.
First off, the difference between:
using namespace std;
...
cout << "Something" << endl;
And using
std::cout << "Something" << std::endl;
Is simply a matter of scope. Scope is just a fancy way of saying how the compiler recognizes names of variables and functions, among other things. A namespace does nothing more than add an extra layer of scope onto all variables within that namespace. When you type using namespace std
, you are taking everything inside of the namespace std
and moving it to the global scope, so that you can use the shorter cout
instead of the more fully-qualified std::cout
.
One thing to understand about namespaces is that they stretch across files. Both <iostream>
and <iomanip>
use the namespace std
. Therefore, if you include both, then the declaration of using namespace std
will operate on both files, and all symbols in both files will be moved to the global scope of your program (or a function's scope, if you used it inside a function).
There are going to be people who tell you "don't use using namespace std
!!!!", but they rarely tell you why. Lets say that I have the following program, where all I am trying to do is define two integers and print them out:
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int cout = 0;
int endl = 1;
cout << cout << endl << endl; // The compiler WILL freak out at this :)
return 0;
}
When I use using namespace std
, I am opening the door for naming collisions. If I (by random chance), have named a variable to be the same thing as what was defined in a header, then your program will break, and you will have a tough time figuring out why.
I can write the same program as before (but get it to work) by not using the statement using namespace std
:
#include <iostream>
int main(int argc, char** argv) {
int cout = 0;
int endl = 1;
std::cout << cout << endl << std::endl; // Compiler is happy, so I'm happy :)
return 0;
}
Hopefully this has clarified a few things.