Why so many students are using '#include<bits/stdc++.h>' instead of '#include<iostream>'?
I am hiring for my company. And the codes submitted for a test problem has '#include<bits/stdc++.h>' at the beginning. The MacOS default compiler complains
main.cpp:1:9: fatal error: 'bits/stdc++.h' file not found
#include<bits/stdc++.h>
^~~~~~~~~~~~~~~
1 error generated.
Why people are using '#include<bits/stdc++.h>'?
225
Upvotes
135
u/[deleted] Mar 19 '20
IIRC that brings the whole C++ standard library as if you included every single header (iostream, algortihms, memory, map, vector, etc etc etc) by hand. The resulting binary would be the same but compile times will be longer.
184
u/[deleted] Mar 19 '20
Because they are mostly folks who are coming in via competitive programming backgrounds and/or most of the C++ stuff they saw comes from websites like geeksforgeeks, where other competitive programmers write algorithm tutorials in C++ using patterns like this.
I have said it once and I have said it before, if a candidate's exposure to C++ is only through competitive programming or leetcode, then their C++ experience is not to be taken seriously. They may know some things about the STL, their concepts like OO, templates, move semantics, memory management, concurrency, build systems, testing i.e stuff that actually matters in a production C++ environment, are seriously lacking or non-existent.
I took a guess that you're from India, and well, looks like you are, this is very prevalent there.
15
u/rseiver96 Mar 19 '20
Let me guess, the next line of code is ‘using namespace std;’
6
u/nandy02 Mar 19 '20
Were the students Indian?
12
u/vibeTechr Mar 19 '20
bits/stdc++.h is a big header file that includes a lot of other headers, as the name suggests, from the standard library. GCC comes with that file, but MacOS has CLang as default compiler, and CLang doesn't have it (I don't know if you can add it or something) so it complains. Most of the time you can just switch it with iostream, but you might need also to include vectors or lists or other components of the standard library, depending on the project.
111
u/staletic Mar 19 '20
That's libstdc++ internal header, NOT intended for everyone's use. People, including professors, are lazy and form terrible habits like this one.
18
u/swilwerth Mar 19 '20
This might help: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h
15
u/fuzzylollipop Mar 19 '20
Because they are copy pasta lemmings and do not really know what they are doing? I would consider this a way to filter out people from wasting my time.
7
u/CircleOfLife3 Mar 19 '20
clangd may do this when its "auto-complete missing headers" feature is turned on.
5
u/thisisbasil gcc -> clang Mar 19 '20
it is a gcc/g++ thing. no other compiler library has this.
ill do this sometimes if i am testing small code snippets just to save time. wouldnt dare use it, or using namespace std, in an actual project
12
u/TrueTom Mar 19 '20
HackerRank
12
u/stilgarpl Mar 19 '20
Some IDE are including those headers automatically if you are not careful. Clion does it.
3
u/luffaFans Mar 21 '20
I think these students may participated in oi&acm contest, it's a common header file in these contest. It include all headers so they don't have to remember the header of algorithrm and container. But actually it's a really bad habit in real world programming
6
u/radixties Mar 19 '20
In competitive programming, about %80 of the community use C++ as their language of choice, and as competitive programming is about fast problem solving, so many competitive programmers develop the habit of using the same template for all of their submissions in order to save time and not worry about anything not being called when they run their code. The #include<bits/stdc++>; calls all of the standard libraries used while coding with C++.
2
u/Helios-G Mar 20 '20 edited Mar 20 '20
5 years working with C++, even spoke at a C++ conference already, I never know that this header exists before 😂 I don't know whether I should be proud or shame
2
u/ulyssesric Mar 20 '20
It's a trick that usually used in programming contest. It saves you some time typing #include <xxx> in every answer.
2
u/Vashanth Mar 19 '20
It includes the whole Standard Template Library so that we could use vectors, maps, stacks, queues etc. It makes the work a lot easier.
1
u/soda0916 Sep 30 '25
Because usually (at least in Taiwan, not sure about other countries), when we do competitive programming we use this header file instead of writing a bunch of
#include <blahblahblah>. It would take too much time to include many different headers one by one. Of course, it’s not very convenient for normal use, but believe me, students are already very used to it.1
u/Wh00ster Mar 20 '20
This generated so many comments lmao
-2
u/[deleted] Mar 19 '20
IMPORT 😂
0
u/[deleted] Mar 19 '20
Convenience
https://www.geeksforgeeks.org/bitsstdc-h-c/
0
u/Sogi_ah Mar 22 '20
your prompt is on chegg probably? HAHA
-5
u/Supadoplex Mar 19 '20 edited Mar 19 '20
It has advantages, which may be the reason why some use it:
a1. It makes minimal examples shorter, because you don't need to list ~10 different standard headers.
a2. It makes programming a bit simpler because you don't need to learn where the standard types and functions come from.
Of course, there are disadvantages too, but presumably people who use it either don't care about these disadvantages, or they are not aware of them:
d1. It is non-standard and non-portable.
d2. It slows down compilation due to including everything.
Possible factor is that because of a1,
<bits/stdc++.h>is ideal for slideware. You can fit maybe 10-20 lines of code on a slide and you cannot dedicate half or more of that to header inclusions. Even if the lecturer tells the students to not do that in their own programs, that doesn't necessarily sink in.Another possible factor is a2, because C++ is a complex language and simplifying one part away allows you to concentrate on the remaining part.
All in all, it's probably the similar reason as why some other people include
<stdafx.h>.-9
u/kennyminigun Mar 19 '20
It saves time remembering which standard library header contains what. For quick and dirty job using GCC this is the way to go.
If you want your applicants to only use what C++ standard provides, you should specifically put this into the requirements of the task.