2
using namespace std;

What is the function of this statement?

Does this have the same function of "include "?

Socob
  • 1,189
  • 1
  • 12
  • 26
CAW
  • 49
  • 1
  • 8
  • In addition to the answers that are given. Nowadays you normally won't use `using namespace` anymore, instead of that, you would only use `using` one those identifiers you want to use without namespace. – t.niese Feb 22 '20 at 09:00
  • Related: [some reasons to avoid it](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice/1452759#1452759) – Nate Eldredge Feb 08 '21 at 01:12

3 Answers3

4

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;.

skratchi.at
  • 1,151
  • 7
  • 22
  • 1
    To save the pain of compiling errors, but grant your the time it would've taken to write 5 characters, you can also simply use `using std::cout;` and `using std::endl` - you'll still need to qualify things like `std::find`, `std::max`, but you can then just write `cout` – Tas Feb 22 '20 at 07:58
  • 1
    @Tas This would be an way. My point of view is, it is better style and taste to use `std::` everywhere. So everybody knows what is going on. Anyway, in the long run, it is just a matter of taste. – skratchi.at Feb 22 '20 at 08:01
1

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
TuanPM
  • 685
  • 8
  • 29
1

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.

Peter
  • 35,646
  • 4
  • 32
  • 74