What does "using namespace std" do? I am new to C++ and have seen it in a lot of source code. Just wondering, Thanks.
What does "using namespace std" do? I am new to C++ and have seen it in a lot of source code. Just wondering, Thanks.
The c++ Standard Library is defined within the "std" namespace. A namespace is a unique declarative region that attaches an additional identifier to any names declared inside it.
namespace foobar
{
int i;
int j;
}
These ints names won't collide because they are foobar::i and foo::i, respectively.
namespace foo
{
int i;
int j;
}
This allows reuse of names and prevents name collisions, which can be a problem with very large projects. In order to get access to names within the "std" namespace (actually any namespce) you either need to use scope resolution operator for the namespace( std::cout ) or access the entire namespace by using the "using namespace std;". The second method allows the use of names within the namespace without using the scope resolution operator such as "cout" not std::.
Now that I've given a long answer to a short question, the using namespace std allows access to the C++ Standard Library without using the scope resolution operator, std::.