eduarc's blog

By eduarc, 11 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

| Write comment?
»
11 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>
  • »
    »
    11 years ago, # ^ |
    Rev. 2   Vote: I like it +19 Vote: I do not like it
    #include <pair>
    

    ???

  • »
    »
    10 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>
    
»
11 years ago, # |
  Vote: I like it +4 Vote: I do not like it

Doesn't work for MS C++ :(

»
11 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.

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

»
11 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!!!

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

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

»
11 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.

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

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

    • »
      »
      »
      11 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.

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

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

        • »
          »
          »
          »
          »
          11 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)"

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

Why does the code not work on ideone?

»
10 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.

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

Why doesn't tourist use it ?!

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

+1

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

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

  • »
    »
    8 years 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!

  • »
    »
    8 years 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.

»
9 years 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)

»
8 years 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.

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

    Yup, it also slows down compilation time.

    • »
      »
      »
      8 years 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.

  • »
    »
    8 years 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.)

»
5 years ago, # |
  Vote: I like it -12 Vote: I do not like it

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:

  • This is an implementation file for a precompiled header.