I understand that if there is no using namespace std
, and you want to write a cout
, you need to have a std::cout
.
What does the std
represent? Why is std
widely used, e.g. std::vector
, std::cout
, and std::cin
?
I understand that if there is no using namespace std
, and you want to write a cout
, you need to have a std::cout
.
What does the std
represent? Why is std
widely used, e.g. std::vector
, std::cout
, and std::cin
?
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.