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:
Header file definition:
So how can I wrap up these library? Suppose I have —
???
First: there is no header file called
<pair>
, std::pair exists in a standard header called<utility>
;Second: you can wrap up all these lines by deleting all of them and writing one line:Doesn't work for MS C++ :(
Just to let all know it also doesn't work in OSX because xcode uses llvm compiler and not gnu.
misunderstood.
The compiler is Clang, LLVM is just the front end. Also by GNU, you probably meant GNU GCC.
Does anyone know how to make it work on OS X?
Yes is described here http://blog.kuoe0.tw/posts/2014/01/31/install-gnu-gcc-on-os-x-and-use-the-header-bits-stdcplusplus-h-and-policy-based-data-structure
Can you please provide the link to same article in english , thanks .
It works! Follow these simple steps:
alias g++="g++-5 --std=c++1y"
That's it.
OK, thanks for fast response!)
still bits/stdc++.h is not working on my mac os x(it's saying file not found ))
maybe you should
$ brew install gcc48
can you explain more
Is this portable in all the programming sites that support g++?
want to hear from experienced C++ coders like "Venco" in topcoder.
The header file is well defined in the GCC documentation. Maybe this help.
http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_headers.html
See the section "Precompiled Headers".
My local files.
Windows MinGW 4.6.1 bits/stdc++.h
Linux GCC 4.8.0 bits/stdc++.h
From my experience, there are some online judges where it does not work, like POJ, but it works in most of them.
can you please explain why it works and meaning of the 2 lines #include <bits/stdc++.h> #define _ ios_base::sync_with_stdio(0);cin.tie(0);
includes all standard headers.
read this: http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_headers.html
is for I/O optimization (cin and cout).
read this: http://codeforces.net/blog/entry/925
There is no effect on time of execution I think.
It never has. This happens pre-exeution. All it saves is typing speed. Unless you have a template.
I tested it on Linux 12.04 CNU c++ and it works. THAAAAAAAAAAAAAAANKS!!!
Thanks. Today I tested this on UVa Online Judge, it worked !
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()
:The
ios_base::Init
object is there to ensure thatcin
is constructed prior to its use.Thanks a lot, much clearer. Can you explain how
struct _ { ... } _;
works?It is idential to
struct _ { }; _ _;
orstruct x { }; x x;
. C++ allows a variable to have the same name as a class.I mean why is it executes in the beginning of program?
Code above is compressed version of this.
Now when myVariable is initialized the default constructor is called executing "cin.sync_with_stdio(0) and cin.tie(0)"
Why does the code not work on ideone?
Which code are you talking about? Code from the entry works well. http://ideone.com/HOh8qI
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?
http://en.wikipedia.org/wiki/C++11
this code works well with both C++ 4.8.1 and C++11 for me.
Why does my console keep on saying "No such file or directory" and "unresolved inclusion"? Thanks for any help.
Why doesn't tourist use it ?!
Why do you care?
+1
seems doesn't work in clang.....
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 <bits/stdc++.h> in your program it will include the file you have just added!
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.
You can see contents of <bits/stdc++.h> here.
As you see, it includes all of the standard library.
But it has not gnu libraries like
<ext/pb_ds/assoc_container.hpp>
(when you want to useordered_set
)For what we need
ordered_set
?Is it treap ?
Read more about
ordered_set
here.Do you know how to use "ext/pb_ds/assoc_container.hpp" in mac?
No.
Hi! Have you figured out any way yet? I still rely on online ide's for this
You'll need
libstdc++
which is the standard library implementation by GCC unlikelibc++
by LLVM. So why not installlibstdc++
?If you need the headers, they are here.
Adding all the files of
include
directory to your include path should do the trick.I installed gcc from homebrew and it worked.
The linked way has worked fine for me.(macOS Catalina). https://solarianprogrammer.com/2019/10/12/compiling-gcc-macos/
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.
Yup, it also slows down compilation time.
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.
How can I do it right?
Create a precompiled header
It is not undefined, it's implementation-defined. (But
include <vector>
is implementation-defined too.)Of course, NOT
#include <vector>
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.pdfWell OP talked about "the result of using a header", which is implementation-defined :)
Using #include<bits/stdc++.h> 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 <bits/stdc++.h> has warned: