using namespace std;
is almost an oximoron, because in a way it defeats the purpose of a namespace. A namespace provides a way to avoid having conflicting names among variables, functions, classes etc. However whenever you say
using namespace std
you will get an error if you were to for example, make the following statement:
The compiler would become confused whenever you try to use the variable endl, because it conflictts with the std namespace. There are two ways to avoid this. First, instead of using the entire standard namespace, just use what you need from it. If you're writing a program that only needs to use cout, endl, strings and ios flags instead of writing
using namespace std
you could write:
1 2 3 4 5
|
using std::cout;
using std::endl;
using std::string;
using std::ios;
using std::iosflag;
|
This avoids unnecessary conflict between names with things you don't intend to use. The second (and in my opinion, better) way to go about this problem is to prepend everything from the std library with
std::
. Things would be written as such:
1 2 3 4 5
|
string string; // old way, note that this would cause errors because the names would conflict
std::string string; // new way, which causes no errors
cout << "hello, world!" << endl; // old way
std::cout << "hello, world!" << std::endl; // new way
|
Hopefully you get the idea now :) I'd recommend using the latter method, just a little more typing that will save you a lot of pain!