I read that #include <bits/stdc++.h> includes all the C++ libraries but is this a correct practice?
Asked
Active
Viewed 57 times
-2
Joaquin Corradi
- 11
- 1
- 1
-
4No, never do that. – πάντα ῥεῖ May 25 '22 at 15:15
-
This is the answer I think is the best there, and I have upvoted it: https://stackoverflow.com/a/31950147/4561887. In short, it is acceptable for programming competitions and rapid code snippets or proofs of concept you are trying to write. Otherwise, don't use it, especially in production code intended for real applications. – Gabriel Staples May 25 '22 at 15:17
-
2@GabrielStaples this also explains why programming competitions should be avoided by beginners: you get some bad habit that will be hard do remove later – Gian Paolo May 25 '22 at 15:22
-
1Even if it is a "competition" `#include
#include – PaulMcKenzie May 25 '22 at 15:27#include ` is typically what those questions require to be answered. If a competitor cannot type in those three lines, maybe they should take a typing course. And if you want to boil it down further, just ` ` is all that's required, and as the code is written, you add the necessary include files (for example, you find you need to use vector later in the development, then you `#include `). Less typing up front. -
One issue is that the header file is not standard and not all compilers accept it. Are you really using all those symbols, or only a few from a few header files? – Thomas Matthews May 25 '22 at 15:27
-
3Also, if that `
` header is included, and then you have `using namespace std;`, you then get into the situation of ambiguous names, such as [data](https://en.cppreference.com/w/cpp/iterator/data), [gcd](https://en.cppreference.com/w/cpp/numeric/gcd), [swap](https://en.cppreference.com/w/cpp/algorithm/swap) and any other "popular" names for variables and functions being an issue when the code is attempted to be compiled. Then the "competitor" is left scratching their head wondering why their code fails to compile. – PaulMcKenzie May 25 '22 at 15:36 -
Code should include the headers that provide the names that the code uses. – Pete Becker May 25 '22 at 15:43
-
@PaulMcKenzie: easy solution to your last one: _never use `using namespace std;`._ That one I for sure agree is bad coding practice. – Gabriel Staples May 25 '22 at 15:51
-
@GabrielStaples please don't even mention, or advise for so called "code competition" sites. It's like you'd advise going for plague, pestilence or worse. – πάντα ῥεῖ May 25 '22 at 15:59
-
@GabrielStaples -- I tried to use `
` in some proof-of-concept code that I wrote; the compiler said it didn't exist. – Pete Becker May 25 '22 at 16:11 -
@GabrielStaples -- Yes, the solution is to never use `using namespace std;`. But look how many of these competitive coding "programmers" throw in the bits header, and the `using namespace std;` just by instinct. You hardly ever see where the bits header is not accompanied with `using namespace std;` -- they are married to each other it seems, in the typical competitive coder's mind. – PaulMcKenzie May 25 '22 at 17:33
-
@PeteBecker, `
` is part of the gcc/g++ compiler. See the section titled _"Where and what is ` – Gabriel Staples May 25 '22 at 19:29`?"_ in my answer here: [How bad is including , really?](https://stackoverflow.com/a/71863759/4561887). You can copy and paste the file directly from the gcc source code online here, so you can use it with your own compiler: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/precompiled/stdc%2B%2B.h. I at least like seeing a list of all headers one can include, and which version of C++ they pertain to, by looking at that header file. -
@GabrielStaples -- so your advice is that I should change compilers? Or reconfigure the one that I have? Sorry, not gonna happen. Writing non-standard code isn't worth the effort. – Pete Becker May 25 '22 at 20:09
-
@PeteBecker, I'm not sure where you got the idea that I'm asking you to change compilers. It's a single file you can copy and paste for use in any compiler. I advised you to copy and paste it and use it in your current compiler, when I said, "You can copy and paste the file directly from the gcc source code online here, **so you can use it with your own compiler**." I also do **not** recommend using it in production code. – Gabriel Staples May 25 '22 at 20:11