Using namespace std | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 30

Using namespace std

Why do we need to write" using namespace std " in c++?

11th Nov 2016, 6:42 AM
Lara - avatar
9 Antworten
+ 21
because other wise you would have to write cout statements like this: std::cout << "first std"; std::cout << "second std"; std::cout << "third std"; std::cout << "i hate STDS!"; So you provide the compiler the information that you will be using the class std very frequently so now that it knows this you don't have to keep on including the std:: in the beginning of each cout or cin statement.
11th Nov 2016, 7:03 AM
J.C Vega
J.C Vega - avatar
+ 7
By writing using namespace std you tell the compiler that you want to use everything in that namespace. Beginners usually use just cin and cout. You can replace using namespace std with using std::cin and using std::cout. If you write using namespace std in a program, there is a possibility that the compiler will crash if you define a variable or a function with the same name with a function/variable from std. This is an important reason why you should specify exactly what you want to use from std.
11th Nov 2016, 7:16 AM
bogdan - avatar
+ 7
The built in C++ library routines are kept in the standard namespace. That includes stuff like cout, cin, string, vector, map, etc. Because these tools are used so commonly, it's popular to add "using namespace std" at the top of your source code so that you won't have to type the std:: prefix constantly.
12th Nov 2016, 2:20 AM
Nouman Danish
Nouman Danish - avatar
+ 5
In simple language what I understood... that iostream is a class and cin and cout are there object. to access cin and cout outside the standart class iostream scope resolution operator is required. To remove this problem we use using namespace std.
12th Nov 2016, 7:56 PM
Jagrati Katariya
Jagrati Katariya - avatar
+ 5
std means standard liberary.
14th Nov 2016, 5:46 PM
omar asif
omar asif - avatar
+ 4
As @bogdan already wrote the short answer is you shouldn't write using entire namespace (that is usualy only used for short toy samples) and I'd say that most of the time you shouldn't even use the partial inclusion. And now for the longer version :) Think of namespaces as classes but with the ability to write their content in different files (you can only write class a{public: class b{}; class c{};}; in one file but you can write namespace a{class b{};} namespace a{class c{}} in two different files - this is the same analogy as folder paths in java: System.out is in c++ namespace System{namespace out{}}). The reason you'd like to group things together is a) because you are writing a library and want to point out to users that some parts make up a whole and are intended to work great together and b) since you write your own library and I write my own but we'd like to use them together I would wrap my library into my namespace and you into yours so even if we both write a class/function with the same name and parameters they won't cause ambiguity in compiler (compiler/linler can't be certain which of the two implementations to use if you include both in the same file so it will spit out a hey you explain better which version you want to use error :)). There are some cases when you want to break the rules (e.g. make namespace shorter: using mod = some::ns::foo::bar::modme; mod::converter c;) but consider them more advanced use cases which are more guidelines of personal taste than anything else (is alias more readable and less confusing than the lenghty original chain?).
17th Nov 2016, 8:29 PM
Domen Vrankar
Domen Vrankar - avatar
+ 2
We write that std library first so that we don't need to define the std library name per namespace like cout, cin , endl and so on.
21st Nov 2016, 8:27 AM
Md Safayet El Hossain
Md Safayet El Hossain - avatar
+ 1
is it compareble with the "uses" in Pascal?
11th Nov 2016, 8:14 AM
Jelle Bleeker
Jelle Bleeker - avatar
In using namespace std , std is a standard library and using std we can perform I/o operations for eg std::cout
13th Nov 2016, 2:57 AM
saurabh sinha
saurabh sinha - avatar