using namespace std;
What is the function of this statement?
Does this have the same function of "include "?
using namespace std;
What is the function of this statement?
Does this have the same function of "include "?
One concept in c++
are namespaces. This organizes your code in a way.
What is using namespace std;
do now? Let us explore this by example.
#include <iostream>
int main() {
std::cout << "Hello World" << std::endl; // important line
return 0;
}
You see the std
keyword in the line with the comment. This is called a namespace. So you tell the compiler, you want to use cout
from the namespace std
.
With using namespace std;
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl; // important line
return 0;
}
There you tell the compiler, to open the std
namespace four you in this scope. So you can use cout
without the std::
. This could be mistaken to be more efficent to code. But you will run into problems at one point.
In the namespace std
are hunderts or thounsands of functions or constants and so on defined. Most of the time you won't bother with that. But if you define a function with the same name and parameters at one point, you hardly will be able to find the mistake. For example there is std::find
. There may be a chance you define the same function. The compiler errors in this case are a pain. So I would strongly discurage you to use using namespace std;
.
It is not same function of "include". This statement allow you access any class, function or type defined in the std namespace without typing "std::" before name of class or function.
Example: if you want to declare a variable of string type in std namespace
Without "using namespace std;"
#include <string>
...
std::string str1;
std::string str2;
with "using namespace std;"
#include <string>
using namespace std;
...
string str1;
string str2
The using directive using namespace std
makes names within namespace std
candidates for matching names used in the current scope.
For example, in
#include <vector>
using namespace std;
int main()
{
vector<int> v;
}
The name vector
exists within namespace std
(as a templated class). In main()
when it sees usage of the name vector
, the previous using namespace std
causes the compiler to look in std
for names that match vector
. It finds the std::vector
, so uses that - and v
then has actual type std::vector<int>
.
The using directive does not have the same function as a preprocessor #include
. In the above, the #include <vector>
is replaced by the preprocessor with the content of the standard header named <vector>
. The content of that header declares the templated std::vector
class and things associated with it.
If you remove the #include <vector>
from the above sample, and leave the using namespace std
in place, the code will not compile, since the compiler does not have visibility of the content of <vector>
.
Conversely, if you remove the using namespace std
but keep the #include <vector>
, the code will not compile, since the name vector
is in namespace std
and that namespace will not be searched for names to match vector
.
Of course, you could change the code above to
#include <vector>
int main()
{
std::vector<int> v;
}
in which case the usage of std::vector
explicitly tells the compiler to look in namespace std
for the name vector
.
There are also circumstances in which it is inadvisable to use using namespace std
, but I'll leave finding out about that as an exercise.