How does #include <bits/stdc++.h> work in C++?
Solution 1
It is basically a header file that also includes every standard library and STL include file. The only purpose I can see for it would be for testing and education.
Se e.g. GCC 4.8.0 /bits/stdc++.h source.
Using it would include a lot of unnecessary stuff and increases compilation time.
Edit: As Neil says, it's an implementation for precompiled headers. If you set it up for precompilation correctly it could, in fact, speed up compilation time depending on your project. (https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html)
I would, however, suggest that you take time to learn about each of the sl/stl headers and include them separately instead, and not use "super headers" except for precompilation purposes.
Solution 2
#include <bits/stdc++.h>
is an implementation file for a precompiled header.
From a software engineering perspective, it is a good idea to minimize the include. If you use <bits/stdc++.h> it actually includes a lot of files, which your program may not need, thus increase both compile-time and program size unnecessarily. [edit: as pointed out by @Swordfish in the comments that the output program size remains unaffected. But still, it's good practice to include only the libraries you actually need, unless it's some competitive competition ]
But in contests, using this file is a good idea, when you want to reduce the time wasted in doing chores; especially when your rank is time-sensitive.
It works in most online judges, programming contest environments, including ACM-ICPC (Sub-Regionals, Regionals, and World Finals) and many online judges.
The disadvantages of it are that it:
- increases the compilation time.
- uses an internal non-standard header file of the GNU C++ library, and so will not compile in MSVC, XCode, and many other compilers
Solution 3
That header file is not part of the C++ standard, is therefore non-portable, and should be avoided.
Moreover, even if there were some catch-all header in the standard, you would want to avoid it in lieu of specific headers, since the compiler has to actually read in and parse every included header (including recursively included headers) every single time that translation unit is compiled.
Solution 4
Unfortunately that approach is not portable C++ (so far).
All standard names are in namespace std
and moreover you cannot know which names are NOT defined by including and header (in other words it's perfectly legal for an implementation to declare the name std::string
directly or indirectly when using #include <vector>
).
Despite this however you are required by the language to know and tell the compiler which standard header includes which part of the standard library. This is a source of portability bugs because if you forget for example #include <map>
but use std::map
it's possible that the program compiles anyway silently and without warnings on a specific version of a specific compiler, and you may get errors only later when porting to another compiler or version.
In my opinion there are no valid technical excuses that explain why this is necessary for the general user: the compiler binary could have all standard namespace built in and this could actually increase the performance even more than precompiled headers (e.g. using perfect hashing for lookups, removing standard headers parsing or loading/demarshalling and so on).
The use of standard headers simplifies the life of who builds compilers or standard libraries and that's all. It's not something to help users.
However this is the way the language is defined and you need to know which header defines which names so plan for some extra neurons to be burnt in pointless configurations to remember that (or try to find and IDE that automatically adds the standard headers you use and removes the ones you don't... a reasonable alternative).
Related videos on Youtube
GorvGoyl
Personal site & blog: https://gourav.io CV: https://gourav.io/cv
Updated on July 08, 2022Comments
-
GorvGoyl almost 2 years
I have read from a codeforces blog that if we add
#include <bits/stdc++.h>
in aC++
program then there is no need to include any other header files. How does#include <bits/stdc++.h>
work and is it ok to use it instead of including individual header files?-
101010 over 9 yearsMost likely this is for educational purposes. I assume that
bits/stdc++.h
includes all C++ headers. -
Marco A. over 9 years
/agree
with the above. Why would have the standard committee bothered with separating functionalities of the STL into multiple headers in the first place if including just one header had been faster, portable, easy and better? -
Yuushi over 9 yearsFrom the header source itself: This is an implementation file for a precompiled header.
-
Neil Kirk over 9 years@MarcoA. Because computers in 1980 had less memory.
-
riv over 9 years@NeilKirk: I used that tool you mentioned, followed the first link, arrived to this page, then saw your comment, and got stuck in a loop.
-
emsr almost 9 yearsThere was an effort way back by none other than BS to essentially standardize a kitchen sink header for new users and for quick toy builds. It died somehow (bad practice for large real projects? I also remember they wanted release and debug to have the same ABI which was unworkable). I can't find the reference paper though. I think there is a legitimate niche for such a header though. Although this should all magically go away when we get modules.
-
-
Neil Kirk over 9 yearsThat's what precompiled headers are for. I have one with pretty much the whole standard library in it. That's just how roll.
-
Robert Allan Hennigan Leahy over 9 years@NeilKirk: Sounds non-standard to me.
-
Neil Kirk over 9 yearsNo it's not. It's an option on most compiler to speed it up.
-
Neil Kirk over 9 yearsIt's for precompiled headers, which decrease compile time! (When set up right)
-
OJFord over 9 yearsI don't think it's very good to learn C++ totally unaware of where things you're using are defined.. at what point do you stop and figure it out?
-
Neil Kirk over 9 years@OllieFord Never, ideally. This is 21st century. Compilers should figure out what parts of the standard library I need, to save me time to code important stuff. But of course people should learn about the headers, as it is part of the language.
-
Zelix over 9 yearsNo, but depending on student age and curriculum and so forth ...
-
Evgeni Sergeev about 8 yearsA slight disadvantage of including it is that it results in having many names in the namespace. On rare occasions this could lead to some hard-to-understand bugs (for a few seconds). For example if one of your variables is named
count
. But I can't think of a specific confusing error example at the moment... -
Jerry Jeremiah over 7 yearsBy definition "most compilers" is non standard - if it were standard then it would be "all compilers" I don't see anywhere in the standard where it talks about how headers are precompiled...
-
abe312 about 7 yearsfor mac users with clang: you won't have stdc++.h preinstalled as it is not standard header. So just paste the following gist in /usr/local/include/bits and you're good to go. gist.github.com/abe312/a078b27b03b6e29f0a19a279ec3265cd
-
Irfy almost 7 yearsI just came here from codechef.com googling the purpose of including this header in various solutions to contest problems. This explanation makes a lot of sense. +1
-
Swordfish almost 6 years@abe312 "If you use it actually includes a lot of files, which your program may not need, thus increase both compile time and program size unnecessarily." including unused definitions and declarations won't affect program size if not used.
-
abe312 almost 6 years@Swordfish what about RAM memory size while compilation?
-
Swordfish almost 6 years@abe312 i didn't say anything about compilation time or memory but just mentioned that "increase [...] program size unnecessarily." is not true.
-
abe312 almost 6 years@Swordfish I just tested it out with 2 files a.cpp and b.cpp // a.cpp #include<bits/stdc++.h> int main(){ return 0; } // b.cpp int main(){ return 1; } // compiler gcc on mac. .out size => 4248 bytes for both. updating the answer now. :)
-
Swordfish almost 6 years@abe312 *rolleyes*
-
Apollys supports Monica over 5 yearsThe main purpose for this header file would be programming competitions. I would recommend avoidiing it in any serious contexts.
-
Jerry Jeremiah over 3 yearsIf we avoided everything that wasn't part of the standard, then we would rewrite everything for every project. At some point using other people's libraries is a good idea even if they aren't part of the standard.
-
Gabriel Staples about 2 yearsYou have been plagiarized by Ayush Govil on GeeksForGeeks, here, without attribution: geeksforgeeks.org/bitsstdc-h-c. Ayush copied from you (punctuation errors and all): "From, software engineering perspective, it is a good idea to minimize the include. If you use it actually includes a lot of files, which your program may not need, thus increases both compile time and program size unnecessarily. " I hate plagiarism. All he has to do is quote and provide a link and I'd be happy.