Блог пользователя eduarc

Автор eduarc, 11 лет назад, По-английски

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
  • Проголосовать: не нравится

»
11 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

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

#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <pair>
  • »
    »
    11 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +19 Проголосовать: не нравится
    #include <pair>
    

    ???

  • »
    »
    10 лет назад, # ^ |
    Rev. 6   Проголосовать: нравится +5 Проголосовать: не нравится

    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 лет назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

Doesn't work for MS C++ :(

»
11 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

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

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

»
11 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
11 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
11 лет назад, # |
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.

  • »
    »
    11 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      11 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

      • »
        »
        »
        »
        11 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

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

        • »
          »
          »
          »
          »
          11 лет назад, # ^ |
          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)"

»
10 лет назад, # |
  Проголосовать: нравится -6 Проголосовать: не нравится

Why does the code not work on ideone?

»
9 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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

»
9 лет назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

Why doesn't tourist use it ?!

»
9 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

+1

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

  • »
    »
    8 лет назад, # ^ |
    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 <bits/stdc++.h> in your program it will include the file you have just added!

  • »
    »
    8 лет назад, # ^ |
      Проголосовать: нравится 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.

»
8 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # |
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 года назад, # |
  Проголосовать: нравится -12 Проголосовать: не нравится

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.