I understand that if there is not using namespace std
, and you want to write a cout
, you need to have a std::cout
. What does the std
represents? Why is std
widely used (std::vector
,std::cout
,std::cin
)?
CodePudding user response:
It's a namespace reserved for the C standard library.
A good choice historically: any token starting with std
is reserved in C. So that meant that early C compilers would not break existing C code. Of course the languages have diverged since then. Etymologically, std
is an abbreviation for standard.
CodePudding user response:
std stands for "standard".
The reason why so much standard stuff goes in the std
namespace is simple: Before namespaces, different code written by different people would often use the same name and cause a conflict. For example, my drink dispenser program from 1994 might have a class ofstream
which is an orange fanta stream. When a new version of C came along and added ofstream
which was an output file stream, my program wouldn't compile any more, or it'd crash.
Okay, orange-fanta-stream is silly, but major operating systems do have C functions called open
, close
, and index
. I'm sure many people have tried to make global variables called open
, and then their programs have crashed.
In C , all the new C standard stuff is inside std::
, so as long I don't call something in my program std
, they can add new stuff inside std::
and it definitely won't cause this problem. Unfortunately all the stuff that C inherits from C is outside std::
, so you still can't make a global variable called open
(on Linux), but at least it's a start.