eduarc's blog

By eduarc, 4 years ago, In English,

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

 
 
 
 
  • Vote: I like it  
  • +72
  • Vote: I do not like it  

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

So how can I wrap up these library? Suppose I have —

#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <pair>
  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it +19 Vote: I do not like it
    #include <pair>
    

    ???

  • »
    »
    3 years ago, # ^ |
    Rev. 6   Vote: I like it +5 Vote: I do not like it

    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:

    #include <bits/stdc++.h>
    
»
4 years ago, # |
  Vote: I like it +4 Vote: I do not like it

Doesn't work for MS C++ :(

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Is this portable in all the programming sites that support g++?

want to hear from experienced C++ coders like "Venco" in topcoder.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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);

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I tested it on Linux 12.04 CNU c++ and it works. THAAAAAAAAAAAAAAANKS!!!

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks. Today I tested this on UVa Online Judge, it worked !

»
4 years ago, # |
Rev. 2   Vote: I like it +4 Vote: I do not like it

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.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks a lot, much clearer. Can you explain how struct _ { ... } _; works?

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      It is idential to struct _ { }; _ _; or struct x { }; x x;. C++ allows a variable to have the same name as a class.

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I mean why is it executes in the beginning of program?

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
          Rev. 5   Vote: I like it +2 Vote: I do not like it

          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)"

»
3 years ago, # |
  Vote: I like it -6 Vote: I do not like it

Why does the code not work on ideone?

»
3 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Why does my console keep on saying "No such file or directory" and "unresolved inclusion"? Thanks for any help.

»
3 years ago, # |
  Vote: I like it +7 Vote: I do not like it

Why doesn't tourist use it ?!

»
3 years ago, # |
  Vote: I like it +1 Vote: I do not like it

+1

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

seems doesn't work in clang.....

  • »
    »
    13 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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!

    • »
      »
      »
      10 months ago, # ^ |
      Rev. 2   Vote: I like it -10 Vote: I do not like it

      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.

  • »
    »
    13 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

»
19 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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 use ordered_set)

  • »
    »
    9 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    For what we need ordered_set ?
    Is it treap ?

»
13 months ago, # |
Rev. 2   Vote: I like it -33 Vote: I do not like it

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.

  • »
    »
    13 months ago, # ^ |
      Vote: I like it -13 Vote: I do not like it

    Yup, it also slows down compilation time.

    • »
      »
      »
      13 months ago, # ^ |
        Vote: I like it +10 Vote: I do not like it

      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.

  • »
    »
    13 months ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    It is not undefined, it's implementation-defined. (But include <vector> is implementation-defined too.)

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Yes it does work :) it's really useful for contests but it is a bad practice to use it in development or interviews.