#include <bits/stdc++.h> 如何在 C++ 中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25311011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does #include <bits/stdc++.h> work in C++?
提问by JerryGoyal
I have read from a codeforcesblog that if we add #include <bits/stdc++.h>
in a C++
program then there is no need to include any other header files. How does #include <bits/stdc++.h>
work and is it ok to use it instead of including individual header files?
我已阅读从codeforces博客,如果我们添加#include <bits/stdc++.h>
的C++
程序则没有必要包括任何其他的头文件。如何#include <bits/stdc++.h>
工作,是否可以使用它而不是包含单个头文件?
采纳答案by Zelix
It is basically a header file that also includes every standard library and STL include file. The only purpose I can see for it would be for testing and education.
它基本上是一个头文件,还包括每个标准库和 STL 包含文件。我能看到的唯一目的是测试和教育。
Se e.g. GCC 4.8.0 /bits/stdc++.h source.
Using it would include a lot of unnecessary stuff and increases compilation time.
使用它会包含很多不必要的东西并增加编译时间。
Edit:As Neil says, it's an implementation for precompiled headers. If you set it up for precompilation correctly it could, in fact, speed up compilation time depending on your project. (https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html)
编辑:正如尼尔所说,它是预编译头文件的实现。如果您正确地将它设置为预编译,它实际上可以根据您的项目加快编译时间。( https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html)
I would, however, suggest that you take time to learn about each of the sl/stl headers and include them separately instead, and not use "super headers" except for precompilation purposes.
但是,我建议您花时间了解每个 sl/stl 标头并单独包含它们,并且除了预编译目的之外不要使用“超级标头”。
回答by abe312
#include <bits/stdc++.h>
is an implementation file for a precompiled header.
#include <bits/stdc++.h>
是预编译头文件的实现文件。
From, software engineering perspective, it is a good idea to minimize the include. If you use it actually includes a lot of files, which your program may not need, thus increase both compile-time and program size unnecessarily. [edit: as pointed out by @Swordfish in the comments that the output program size remains unaffected. But still, it's good practice to include only the libraries you actually need, unless it's some competitive competition]
从软件工程的角度来看,最小化包含是一个好主意。如果您使用它实际上包含许多您的程序可能不需要的文件,则不必要地增加编译时间和程序大小。[编辑:正如@Swordfish 在评论中指出的那样,输出程序大小不受影响。但是,最好只包含您实际需要的库,除非这是一些竞争性的竞争]
But in contests, using this file is a good idea, when you want to reduce the time wasted in doing chores; especially when your rank is time-sensitive.
但是在比赛中,当你想减少浪费在做家务上的时间时,使用这个文件是个好主意;尤其是当您的排名对时间敏感时。
It works in most online judges, programming contest environments, including ACM-ICPC (Sub-Regionals, Regionals, and World Finals) and many online judges.
它适用于大多数在线评委、编程竞赛环境,包括 ACM-ICPC(分区域赛、区域赛和世界总决赛)和许多在线评委。
The disadvantages of it are that it:
它的缺点是:
- increases the compilation time.
- uses an internal non-standard header file of the GNU C++ library, and so will not compile in MSVC, XCode, and many other compilers
- 增加编译时间。
- 使用 GNU C++ 库的内部非标准头文件,因此不会在 MSVC、XCode 和许多其他编译器中编译
回答by Robert Allan Hennigan Leahy
That header file is not part of the C++ standard, is therefore non-portable, and should be avoided.
该头文件不是 C++ 标准的一部分,因此不可移植,应避免使用。
Moreover, even if there were some catch-all header in the standard, you would want to avoid it in lieu of specific headers, since the compiler has to actually read in and parse every included header (including recursively included headers) every single time that translation unit is compiled.
此外,即使标准中有一些包罗万象的头文件,您也希望避免使用它来代替特定的头文件,因为编译器每次都必须实际读入并解析每个包含的头文件(包括递归包含的头文件)编译单元编译。
回答by Vivek Ghanchi
It is basically a header file that includes every standard library. In programming contests, using this file is a good idea, when you want to reduce the time wasted in doing chores; especially when your rank is time-sensitive. In programming contests, people do focus more on finding an algorithm to solve a problem than on software engineering. From, software engineering perspective, it is a good idea to minimize the include. If you use it actually includes a lot of files, which your program may not need, thus increases both compile-time and program size unnecessarily.
它基本上是一个包含每个标准库的头文件。在编程比赛中,当你想减少浪费在做家务上的时间时,使用这个文件是个好主意;尤其是当您的排名对时间敏感时。在编程竞赛中,人们确实更关注寻找解决问题的算法,而不是软件工程。从软件工程的角度来看,最小化包含是一个好主意。如果您使用它实际上包含很多文件,您的程序可能不需要这些文件,从而不必要地增加编译时间和程序大小。
Visit thisfor more detail.
访问此了解更多详情。
This is best thing for Online coding where speed matters a lot.
这是速度非常重要的在线编码的最佳选择。
回答by Linkon
It is basically a header file that includes every standard library.
In programming contests, using this file is a good idea, when you want to reduce the time wasted in doing chores; especially when your rank is time sensitive.
In programming contests, people do focus more on finding algorithm to solve a problem than on software engineering.
But in software engineeringperspective, it is not a good idea to use. If you use it actually includes a lot of files, which your program may not need, thus increases both compile time and program size unnecessarily.
它基本上是一个包含每个标准库的头文件。
在编程比赛中,当你想减少浪费在做家务上的时间时,使用这个文件是个好主意;尤其是当您的排名对时间敏感时。
在编程竞赛中,人们确实更关注寻找解决问题的算法,而不是软件工程。
但是从软件工程的角度来看,使用它并不是一个好主意。如果您使用它实际上包含很多文件,您的程序可能不需要这些文件,从而不必要地增加编译时间和程序大小。
回答by 6502
Unfortunately that approach is not portable C++ (so far).
不幸的是,这种方法不是可移植的 C++(到目前为止)。
All standard names are in namespace std
and moreover you cannot know which names are NOTdefined by including and header (in other words it's perfectly legal for an implementation to declare the name std::string
directly or indirectly when using #include <vector>
).
所有标准名称都在命名空间中std
,而且您无法知道哪些名称不是由 include 和 header 定义的(换句话说,实现std::string
在使用时直接或间接声明名称是完全合法的#include <vector>
)。
Despite this however you are required by the language to know and tell the compiler which standard header includes which part of the standard library. This is a source of portability bugs because if you forget for example #include <map>
but use std::map
it's possible that the program compiles anyway silently and without warnings on a specific version of a specific compiler, and you may get errors only later when porting to another compiler or version.
尽管如此,语言要求您知道并告诉编译器哪个标准头文件包含标准库的哪个部分。这是可移植性错误的来源,因为例如,如果您忘记了#include <map>
但使用std::map
它,则程序可能会以静默方式编译,并且在特定编译器的特定版本上没有警告,并且只有在稍后移植到另一个编译器或版本时才可能出现错误。
In my opinion there are no valid technical excuses because this is necessary for the general user: the compiler binary could have all standard namespace built in and this could actually increase the performance even more than precompiled headers (e.g. using perfect hashing for lookups, removing standard headers parsing or loading/demarshalling and so on).
在我看来,没有有效的技术借口,因为这对一般用户来说是必要的:编译器二进制文件可以内置所有标准命名空间,这实际上可以比预编译头文件提高性能(例如,使用完美散列进行查找,删除标准标头解析或加载/解组等)。
The use of standard headers simplifies the life of who builds compilers or standard libraries and that's all. It's not something to help users.
标准头文件的使用简化了构建编译器或标准库的人的生活,仅此而已。这不是帮助用户的东西。
However this is the way the language is defined and you need to know which header defines which names so plan for some extra neurons to be burnt in pointless configurations to remember that (or try to find and IDE that automatically adds the standard headers you use and removes the ones you don't... a reasonable alternative).
然而,这是语言的定义方式,您需要知道哪个头文件定义了哪些名称,因此计划在无意义的配置中烧毁一些额外的神经元以记住这一点(或尝试查找自动添加您使用的标准头文件的 IDE 和删除那些你不......一个合理的选择)。