
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How does #include <bits/stdc++.h> work in C++?
In C++, the <bits/stdc++.h> is a header file. This file includes all standard library. Sometimes in some coding contests, when we have to save time while solving, then using this header file is helpful.
In software engineering approach we should reduce the minimize the include. Using this header file, it will include lots of files, sometimes that may not be required in the program. So it may increase the compile time and program size.
The disadvantages of this header file is listed below.
-
This <bits.stc++.h> is not a standard header file of GNU C++ library. So some compiler may fail to compiler source code with this header file.
-
Using this, it may require unnecessary longer time to compile.
-
As this is not a part of standard C++ library so it is non;portable
-
For this header file, every time the compiler tries to import the headers recursively every time the code is compiled.
This header file works by including all standard C++ libraries. The two important lines to note are: first, its inclusion of many standard headers, and second, the convenience it provides for writing quick programs. Below are different examples showing its usage.
Basic Usage
This is simple program that includes <bits/stdc++.h> to print "Hello World". The first line includes the header, which brings in all the standard libraries, and the second line prints the message using cout.
Example
By using <bits/stdc++.h> header file, we print the "Hello World" as a basic demonstration of the output.
#include<bits/stdc++.h> using namespace std; int main() { cout << "Hello World" << endl; return 0; }
Output
Following is the output to the above program:
Hello World
Using STL Data Structures
Here, we demonstrates that including <bits/stdc++.h> gives access to all Standard Template Library (STL) containers like set and map without including their individual headers.
Example
In this example, we store the unique names in a set and print them in sorted order.
#include<bits/stdc++.h> using namespace std; int main() { set<string> names; names.insert("Revathi"); names.insert("Tapas"); names.insert("Sudhir"); for(auto &name : names) { cout << name << endl; } return 0; }
Output
Following is the output to the above program:
Revathi Sudhir Tapas
Using Algorithms and Functions
This approach shows how functions from the algorithm library, like sort work without the need to include the header explicitly. This is because <bits/stdc++.h> includes them automatically.
Example
In ascending order, we sort an array of integers and prints that sorted elements.
#include<bits/stdc++.h> using namespace std; int main() { int arr[] = {4,2,5,1,3}; int n = sizeof(arr)/sizeof(arr[0]); sort(arr, arr + n); for(int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; return 0; }
Output
Following is the output to the above program:
1 2 3 4 5