Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of "using namespace std"? [duplicate]

What is the use of using namespace std?

I'd like to see explanation in Layman terms.

like image 518
Jarvis Avatar asked Sep 20 '13 10:09

Jarvis


People also ask

What is the use of using namespace std?

The using namespace statement just means that in the scope it is present, make all the things under the std namespace available without having to prefix std:: before each of them.

Why do we use using namespace std in C ++?

Need of namespace: As the same name can't be given to multiple variables, functions, classes, etc. in the same scope. So to overcome this situation namespace is introduced.

What is the benefits of using namespace?

Advantages of namespace In one program, namespace can help define different scopes to provide scope to different identifiers declared within them. By using namespace - the same variable names may be reused in a different program.


1 Answers

  • using: You are going to use it.
  • namespace: To use what? A namespace.
  • std: The std namespace (where features of the C++ Standard Library, such as string or vector, are declared).

After you write this instruction, if the compiler sees string it will know that you may be referring to std::string, and if it sees vector, it will know that you may be referring to std::vector. (Provided that you have included in your compilation unit the header files where they are defined, of course.)

If you don't write it, when the compiler sees string or vector it will not know what you are refering to. You will need to explicitly tell it std::string or std::vector, and if you don't, you will get a compile error.

like image 62
Daniel Daranas Avatar answered Oct 12 '22 18:10

Daniel Daranas