### eduarc's blog

By eduarc, 7 years ago, ,

Hi! See this useful C++ include directive.

#include <bits/stdc++.h>


This let you use any C/C++ standard library without adding extra "includes". No more compilation errors because missing libraries :)

And you can make a shorter version of your template. For example, my template.

  #include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

using namespace std;

int main() { _

return 0;
}


I tested it with the GNU/C++ compiler.

More information:

Headers — GCC Documentation

Header file definition:

Linux GCC 4.8.0

Windows MinGW 4.6.1

• +72

 » 7 years ago, # |   +5 So how can I wrap up these library? Suppose I have — #include #include #include #include #include 
•  » » 7 years ago, # ^ | ← Rev. 2 →   +19 #include ???
•  » » 6 years ago, # ^ | ← Rev. 6 →   +5 First: there is no header file called , std::pair exists in a standard header called  ;Second: you can wrap up all these lines by deleting all of them and writing one line: #include 
 » 7 years ago, # |   +4 Doesn't work for MS C++ :(
•  » » 7 years ago, # ^ | ← Rev. 2 →   +5 Just to let all know it also doesn't work in OSX because xcode uses llvm compiler and not gnu.
•  » » » 7 years ago, # ^ | ← Rev. 2 →   0 misunderstood.
•  » » » 6 years ago, # ^ |   +3 The compiler is Clang, LLVM is just the front end. Also by GNU, you probably meant GNU GCC.
•  » » » 6 years ago, # ^ |   0 Does anyone know how to make it work on OS X?
•  » » » » 6 years ago, # ^ |   0
•  » » » » » 5 years ago, # ^ |   0 Can you please provide the link to same article in english , thanks .
•  » » » » » » 5 years ago, # ^ |   0
•  » » 4 years ago, # ^ | ← Rev. 3 →   +5 It works! Follow these simple steps: Install gcc using homebrew or macports. Open the bash_profile present in home directory (~/.bash_profile) and add the following line at the end of file: alias g++="g++-5 --std=c++1y" Run the command: source ~/.bash_profile That's it.
•  » » » 4 years ago, # ^ |   +43 OK, thanks for fast response!)
•  » » » 4 years ago, # ^ | ← Rev. 2 →   0 still bits/stdc++.h is not working on my mac os x(it's saying file not found ))
•  » » » » 4 years ago, # ^ |   0 maybe you should $brew install gcc48 •  » » » » » 4 years ago, # ^ | 0 can you explain more  » 7 years ago, # | +5 Is this portable in all the programming sites that support g++?want to hear from experienced C++ coders like "Venco" in topcoder. •  » » 7 years ago, # ^ | +1 The header file is well defined in the GCC documentation. Maybe this help.http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_headers.htmlSee the section "Precompiled Headers".My local files.Windows MinGW 4.6.1 bits/stdc++.hLinux GCC 4.8.0 bits/stdc++.h •  » » 7 years ago, # ^ | +1 From my experience, there are some online judges where it does not work, like POJ, but it works in most of them.  » 7 years ago, # | 0 can you please explain why it works and meaning of the 2 lines #include #define _ ios_base::sync_with_stdio(0);cin.tie(0); •  » » 7 years ago, # ^ | 0  #include includes all standard headers.  #define _ ios_base::sync_with_stdio(0);cin.tie(0); is for I/O optimization (cin and cout).read this: http://codeforces.com/blog/entry/925 •  » » » 7 years ago, # ^ | -18 There is no effect on time of execution I think. •  » » » » 7 years ago, # ^ | 0 It never has. This happens pre-exeution. All it saves is typing speed. Unless you have a template.  » 7 years ago, # | 0 I tested it on Linux 12.04 CNU c++ and it works. THAAAAAAAAAAAAAAANKS!!!  » 7 years ago, # | 0 Thanks. Today I tested this on UVa Online Judge, it worked !  » 7 years ago, # | ← Rev. 2 → +4 You can also use a constructor in a global object to set up iostreams. This way you don't have to add anything in main(): struct _ { ios_base::Init i; _() { cin.sync_with_stdio(0); cin.tie(0); } } _; int main() { }The ios_base::Init object is there to ensure that cin is constructed prior to its use. •  » » 7 years ago, # ^ | 0 Thanks a lot, much clearer. Can you explain how struct _ { ... } _; works? •  » » » 7 years ago, # ^ | 0 It is idential to struct _ { }; _ _; or struct x { }; x x;. C++ allows a variable to have the same name as a class. •  » » » » 7 years ago, # ^ | 0 I mean why is it executes in the beginning of program? •  » » » » » 7 years ago, # ^ | ← Rev. 5 → +2 Code above is compressed version of this. struct MyStruct { ios_base::Init i; //i'm not sure but i don't think this line is needed MyStruct() { cin.sync_with_stdio(0); cin.tie(0); }; }; MyStruct myVariable; Now when myVariable is initialized the default constructor is called executing "cin.sync_with_stdio(0) and cin.tie(0)"  » 6 years ago, # | -6 Why does the code not work on ideone? •  » » 6 years ago, # ^ | 0 Which code are you talking about? Code from the entry works well. http://ideone.com/HOh8qI •  » » » 6 years ago, # ^ | 0 I was compiling it in C++ 4.8.1 instead of C++11, maybe that is the reason for the compile time error.Also, What are the major differences between the two? •  » » » » 6 years ago, # ^ | 0 •  » » » » 6 years ago, # ^ | 0 this code works well with both C++ 4.8.1 and C++11 for me.  » 5 years ago, # | +1 Why does my console keep on saying "No such file or directory" and "unresolved inclusion"? Thanks for any help.  » 5 years ago, # | +7 Why doesn't tourist use it ?! •  » » 5 years ago, # ^ | 0 tourist use it — You can see his last 3 contest :) •  » » 5 years ago, # ^ | 0 Why do you care?  » 5 years ago, # | +1 +1  » 5 years ago, # | 0 seems doesn't work in clang..... •  » » 4 years ago, # ^ | ← Rev. 2 → 0 At least if you are on Mac OSX the solution is simple. You go to folder /Library/Developer/CommandLineTools/usr/include/c++/v1/bits , at least on two Macs I have seen this is the folder, for some others it might be different. And then copy the stdc++.h file from here into this folder. Also it is likely that the bits folder will not exist, so when you go to /Library/Developer/CommandLineTools/usr/include/c++/v1/ you might first have to create bits folder in there. Then when you put #include in your program it will include the file you have just added! •  » » » 4 years ago, # ^ | ← Rev. 2 → -10 Please don't ever advice this sort of thing. bits/stdc++.h is not portable and therefore must be specific to GNU GCC (as is). Your solution does well unless people use custom compilers and/or different versions of different compilers, which usually use different$INCLUDE_PATH.Rule of thumb: unless it's portable, avoid it.
•  » » 4 years ago, # ^ |   0 For Xcode Users on Mac, you can use /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/bits path to add file.
 » 4 years ago, # |   0 You can see contents of here.As you see, it includes all of the standard library.But it has not gnu libraries like (when you want to use ordered_set)
•  » » 3 years ago, # ^ |   0 For what we need ordered_set ? Is it treap ?
•  » » » 3 years ago, # ^ |   0 Read more about ordered_set here.
•  » » 2 months ago, # ^ |   +1 Do you know how to use "ext/pb_ds/assoc_container.hpp" in mac?
•  » » » 2 months ago, # ^ |   0 No.
•  » » » 6 weeks ago, # ^ |   0 Hi! Have you figured out any way yet? I still rely on online ide's for this
•  » » » » 6 weeks ago, # ^ |   0 You'll need libstdc++ which is the standard library implementation by GCC unlike libc++ by LLVM. So why not install libstdc++?If you need the headers, they are here.Adding all the files of include directory to your include path should do the trick.
•  » » » » 5 weeks ago, # ^ |   +5 I installed gcc from homebrew and it worked.
•  » » » » 4 weeks ago, # ^ |   0 The linked way has worked fine for me.(macOS Catalina). https://solarianprogrammer.com/2019/10/12/compiling-gcc-macos/
 » 4 years ago, # | ← Rev. 2 →   -33 Actually it isn't portable. The result of using this header file is undefined in C++ Standard. Please don't use it in contests unless you are sure about the compiler. Please visit https://github.com/cplusplus/draft to see if it is actually in the standard.
•  » » 4 years ago, # ^ |   -13 Yup, it also slows down compilation time.
•  » » » 4 years ago, # ^ |   +10 It slows down compilation on the judge but speeds up compilation on your local machine (if you do it right). In fact, speeding up compilation is the whole point of this file.
•  » » » » 4 years ago, # ^ |   0 How can I do it right?
•  » » » » » 4 years ago, # ^ |   +5 Create a precompiled header
•  » » 4 years ago, # ^ |   +2 It is not undefined, it's implementation-defined. (But include  is implementation-defined too.)
•  » » » 4 years ago, # ^ | ← Rev. 4 →   0 Of course, NOT#include  is defined by standard of C++98, C++11 and etc (ISO/IEC 14882:1998, ISO/IEC 14882:2011). It's defined behaviour, not implementation-defined.It's is defined in 17.6.1.2 Headers here https://github.com/cplusplus/draft/blob/master/papers/n4594.pdf
•  » » » » 4 years ago, # ^ |   0 Well OP talked about "the result of using a header", which is implementation-defined :)
 » 4 years ago, # |   0 Yes it does work :) it's really useful for contests but it is a bad practice to use it in development or interviews.
 » 6 weeks ago, # |   -12 Using #include has advantages like We only need one line include We dont need to take care about the libraries But everything has 2 sides, the disadvantages is need to be aware It may not work in some compiler and upgrade compiler may break the program It contain many libraries you dont need, which can make the compiler run longer to access all the functions insides all the libreary. And there is the fact that has warned: This is an implementation file for a precompiled header.