I am learning C++ and in my book using namespace std
is written in every program.
I understand that std::cout <<"hello";
can be simply written as cout << "hello";
by using namespace std.
Why?
Namespaces are a way of avoiding conflicting identifiers when you include multiple headers. It’s very easy to imagine that when you are using a bunch of different libraries, two of them will have a class or function with the same name, and without namespaces, things won’t compile.
Now about using namespace std. There’s no good reason to do this outside of convenience. I would recommend never putting “using namespace std” in your headers. It opens up the possibility for a massive amount of identifiers in the std library to conflict with your own, so you might run into errors while you are coding.
But, you can use the “using” keyword for a narrower scope. If you don’t want to type std::cout all the time, you can put “using std::cout;” and this has the same effect, without as much risk.
thank you for the explanation. Its helpful
deleted by creator
thank you for the link
I watched a video and it mentioned the purpose is to avoid naming conflicts which I didn’t understand.