0

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?

cigien
  • 57,834
  • 11
  • 73
  • 112
  • Std means standard :) – Alvov1 Jul 12 '22 at 10:58
  • `std` is an abbreviation of STandarD. It's the namespace that all standard library classes, functions and templates are put in. – Some programmer dude Jul 12 '22 at 10:59
  • `std` stands for "standard", and it represents the standard library of C++ – Christian Vincenzo Traina Jul 12 '22 at 10:59
  • Click the `std` tag of the question :) – Alon Adler Jul 12 '22 at 10:59
  • Voting to reopen. The linked question is a different question. (or not: today I learned there's a reopen hammer) – user253751 Jul 12 '22 at 10:59
  • Also, [`using namespace std;` is a bad habit](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Some programmer dude Jul 12 '22 at 10:59
  • It is a namespace. – Quimby Jul 12 '22 at 11:00
  • it's a namespace, specifically the one reserved for the standard library. You can also define your own namespaces: https://en.cppreference.com/w/cpp/language/namespace – JHBonarius Jul 12 '22 at 11:00
  • Cultural reference: https://en.wikipedia.org/wiki/C%2B%2B_Standard_Library - Programmer reference: https://en.cppreference.com/w/ – Christian Vincenzo Traina Jul 12 '22 at 11:00
  • Unfortunately not available as duplicate: https://stackoverflow.com/questions/67411397/what-does-using-namespacestd-mean – πάντα ῥεῖ Jul 12 '22 at 11:01
  • I gave myself five minutes to come up with a wisecrack about the more widely known, general, alternative acronym for std, but couldn't do it on such a short notice... – Sam Varshavchik Jul 12 '22 at 11:01
  • @SamVarshavchik: Steady there Sam. Did you avoid it studiously? – Bathsheba Jul 12 '22 at 11:02
  • 2
    [DUPE1: What does using namespace::std mean?](https://stackoverflow.com/questions/67411397/what-does-using-namespacestd-mean), [DUPE2: What exactly is a namespace and why is it necessary](https://stackoverflow.com/questions/32161199/what-exactly-is-a-namespace-and-why-is-it-necessary), [DUPE3: Why std::cout instead of simply cout?](https://stackoverflow.com/questions/10950083/why-stdcout-instead-of-simply-cout) – Jason Jul 12 '22 at 11:02
  • @user253751: Don't go all power crazy on us will you? ;-) – Bathsheba Jul 12 '22 at 11:04
  • @scissors127 Refer to a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). This is explained in literally every beginner level c++ book. – Jason Jul 12 '22 at 11:06
  • 5
    As per [std](https://www.acronymfinder.com/STD.html) stands for **standard**, or **sexually transmitted disease**, or **short term disability**, or **Star Trek: Discovery**, or a few other things. In the context of C++, I think it's the second one. – Eljay Jul 12 '22 at 11:08
  • 5
    @Eljay: The second one?! – Bathsheba Jul 12 '22 at 11:17
  • I thought that `std` is the namespace of STL, the C++ Standard Type Library. – Dominique Jul 12 '22 at 11:38
  • 1
    @Dominique Nope. The STL was a library that was designed in the 90s, and its design influenced the C++ standard library (the library described in ratified ANSI or ISO C++ standards). The STL and the C++ standard library have some features in common, but they are not the same thing. Most of the C++ standard library (there are some things where this is not true) is placed, by the C++ standard, into a namespace called `std`. `std` is just a shorthand for "standard". – Peter Jul 12 '22 at 11:51
  • 10
    @Eljay I've never, AFAIK, caught a sexually transmitted disease as a result of doing software development in C++. In fact, I think that knowing something about C++ contributes to a lower susceptibility ;-) – Peter Jul 12 '22 at 11:55

1 Answers1

5

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.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • 1
    Could've closed this by adding more accurate dupes instead of reopening it. This is a very common SO question. There are plenty of question for this. – Jason Jul 12 '22 at 11:08
  • 6
    @AnoopRana I thought so too, but when I looked, I found that most of them are asking what a namespace is, or how to use a namespace, not *why* a namespace is. – user253751 Jul 12 '22 at 11:08
  • See [here](https://stackoverflow.com/a/67411572/12002570) it is already explained: *"`std` is an abbreviation of "standard". `std` is the "standard namespace". `cout`, `cin` and a lot of other functions are defined within it"* – Jason Jul 12 '22 at 11:10
  • @AnoopRana this answer says "std stands for standard" but it doesn't say why std is widely used - the rest of the answer is how to use a namespace. – user253751 Jul 12 '22 at 11:11
  • The fundamental reason for using a namespace is the same. It doesn't matter what abbreviation you use for the namespace itself. If one understands what are namespace and why we should/shouldn't use them then one automatically understands why `std` is used as well. Ofcourse there may also be historical reasons for using `std` and not some other abbreviation. – Jason Jul 12 '22 at 11:12
  • 1
    @AnoopRana the answer you linked does not explain the reason for using a namespace. Nor do any of the others I looked at. It only explains the abbreviation, as well as how to use it. – user253751 Jul 12 '22 at 11:25