+ 2

Using namespace std; for what it is use? I can't get it.

20th Jun 2019, 5:59 PM
Poonam Patil
Poonam Patil - avatar
3 Réponses
+ 6
Namespaces were introduced in c++ to resolve identifier/ variable name conflicts. This ensured that two objects can have the same name and yet be treated differently if they belonged to different namespaces. Std is a namespace which contains so many predefined identifiers like cout, cin, string, vector etc. If we don't write "using namespace std" and use a statement - cout<<"hello"; in our code then compiler would treat cout as undeclared variable and would throw an error. So, basically , "using namespace std" tells the compiler to check std namespace when resolving the undeclared identifiers. std::cout<<"hello"; would also work.
20th Jun 2019, 8:24 PM
Himani
+ 2
" Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries" source: " https://docs.microsoft.com/en-us/cpp/cpp/namespaces-cpp?view=vs-2019 ". They are similar to python's import statements in that they allow you to access code from other libraries.
20th Jun 2019, 7:33 PM
Nicky
Nicky - avatar
+ 2
It imports all the items in the standard namespace located in the standard library. Without it you would have to type std:: in front of any std library member that you need to use, EG. std::cout std::endl std::string etc... You can create your own namespaces which can lead to bugs if you use the std namespace. See TheCherno on youtube, he has an excellent c++ course where he explains it well.
20th Jun 2019, 7:39 PM
Mike
Mike - avatar