C++ 为什么我不应该#include <bits/stdc++.h>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31816095/
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
Why should I not #include <bits/stdc++.h>?
提问by Lightness Races in Orbit
I posted a question with my code whose only #include
directive was the following:
我用我的代码发布了一个问题,其唯一#include
指令如下:
#include <bits/stdc++.h>
My teacher told me to do this, but in the comments section I was informed that I shouldn't.
我的老师告诉我这样做,但在评论部分我被告知我不应该这样做。
Why?
为什么?
回答by Lightness Races in Orbit
Including <bits/stdc++.h>
appears to be an increasingly common thing to see on Stack Overflow, perhaps something newly added to a national curriculum in the current academic year.
包括<bits/stdc++.h>
似乎是在 Stack Overflow 上越来越常见的事情,也许是本学年新添加到国家课程中的内容。
I imagine the advantages are vaguely given thus:
我想这些优点是模糊地给出的:
- You only need write one
#include
line - You do not need to look up which standard header everything is in
- 你只需要写
#include
一行 - 您不需要查找所有内容都在哪个标准标题中
Unfortunately, this is a lazy hack, naming a GCC internal header directly instead of individual standard headers like <string>
, <iostream>
and <vector>
. It ruins portability and fosters terrible habits.
不幸的是,这是一个懒惰的黑客,直接命名 GCC 内部标头而不是单个标准标头,如<string>
,<iostream>
和<vector>
。它破坏了便携性并养成了糟糕的习惯。
The disadvantages include:
缺点包括:
- It will probably only work on that compiler
- You have no idea what it'll do when you use it, because its contents are not set by a standard
- Even just upgrading your compiler to its own next version may break your program
- Every single standard header must be parsed and compiled along with your source code, which is slow and results in a bulky executable under certain compilation settings
- 它可能只适用于该编译器
- 你不知道当你使用它时它会做什么,因为它的内容不是由标准设置的
- 即使只是将你的编译器升级到它自己的下一个版本也可能会破坏你的程序
- 每个标准头文件都必须与源代码一起解析和编译,这很慢,并且在某些编译设置下会导致可执行文件庞大
Don't do it!
不要这样做!
More information:
更多信息:
- #include <bits/stdc++.h> with visual studio does not compile
- How does #include <bits/stdc++.h> work in C++?
Example of why Quora is bad:
Quora 不好的例子:
回答by Reinstate Monica
Why? Because it is used as if it was supposed to be a C++ standard header, but no standard mentions it. So your code is non-portable by construction. You won't find any documentation for it on cppreference. So it might as well not exist. It's a figment of someone's imagination :)
为什么?因为它被用作好像它应该是一个 C++ 标准头文件,但没有标准提到它。因此,您的代码在构造上是不可移植的。您不会在cppreference上找到任何相关文档。所以它也可能不存在。这是某人的想象力的虚构:)
I have discovered - to my horror and disbelief - that there is a well-known tutorial site where every C++ example seems to include this header. The world is mad. That's the proof.
我发现 - 令我震惊和难以置信 - 有一个著名的教程站点,其中每个 C++ 示例似乎都包含此 header。世界疯了。这就是证据。
To anyone writing such "tutorials"
致任何编写此类“教程”的人
Please stop using this header. Forget about it. Don't propagate this insanity. If you're unwilling to understand why doing this is Wrong, take my word for it. I'm not OK being treated as a figure of authority on anything at all, and I'm probably full of it half the time, but I'll make an exception in this one case only. I claim that I know what I'm talking about here. Take me on my word. I implore you.
请停止使用此标题。忘掉它。不要传播这种疯狂。如果您不愿意理解为什么这样做是错误的,请相信我的话。在任何事情上我都不能被视为权威人物,我可能有一半的时间都充满了它,但我只会在这种情况下破例。我声称我知道我在这里谈论的是什么。相信我的话。我恳求你。
P.S. I can well imagine the abominable "teaching standard" where this wicked idea might have taken place, and the circumstances that led to it. Just because there seemed to be a practical need for it doesn't make it acceptable - not even in retrospect.
PS我可以想象这个邪恶想法可能发生的可恶的“教学标准”,以及导致它的环境。仅仅因为它似乎有实际需要并不能让它被接受——即使回想起来也是如此。
P.P.S. No, there was no practical need for it. There aren't that many C++ standard headers, and they are well documented. If you teach, you're doing your students a disservice by adding such "magic". Producing programmers with a magical mindset is the last thing we want. If you need to offer students a subset of C++ to make their life easier, just produce a handout with the short list of headers applicable to the course you teach, and with concise documentation for the library constructs you expect the students to use.
PPS 不,没有实际需要。没有那么多 C++ 标准头文件,而且它们都有很好的文档记录。如果你教书,你就是在通过添加这样的“魔法”来伤害你的学生。培养具有神奇思维的程序员是我们最不想要的。如果您需要为学生提供 C++ 的一个子集以使他们的生活更轻松,只需制作一份讲义,其中包含适用于您教授的课程的简短标题列表,以及您希望学生使用的库结构的简明文档。
回答by RedGreenCode
There's a Stack Exchange site called Programming Puzzles & Code Golf. The programming puzzleson that site fit this definition of puzzle:
有一个叫做Programming Puzzles & Code Golf的 Stack Exchange 站点。该站点上的编程谜题符合以下谜题的定义:
a toy, problem, or other contrivance designed to amuse by presenting difficulties to be solved by ingenuity or patient effort.
一种玩具、问题或其他设计,旨在通过提出需要通过独创性或耐心努力解决的困难来娱乐。
They are designed to amuse, and not in the way that a working programmer might be amused by a real-world problem encountered in their daily work.
它们旨在娱乐,而不是像工作程序员可能会被日常工作中遇到的现实世界问题逗乐的方式。
Code Golfis "a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that implements a certain algorithm." In the answers on the PP&CG site, you'll see people specify the number of bytes in their answers. When they find a way to shave off a few bytes, they'll strike out the original number and record the new one.
Code Golf是“一种娱乐性的计算机编程比赛,参赛者努力实现实现特定算法的尽可能短的源代码。” 在 PP&CG 站点上的答案中,您会看到人们在他们的答案中指定了字节数。当他们找到减少几个字节的方法时,他们会删除原始数字并记录新数字。
As you might expect, code golfing rewards extreme programming language abuse. One-letter variable names. No whitespace. Creative use of library functions. Undocumented features. Nonstandard programming practices. Appalling hacks.
正如您所料,代码高尔夫球会奖励极端的编程语言滥用。一个字母的变量名。没有空格。创造性地使用库函数。未记录的功能。非标准编程实践。骇人听闻的黑客攻击。
If a programmer submitted a pull request at work containing golf-style code, it would be rejected. Their co-workers would laugh at them. Their manager would drop by their desk for a chat. Even so, programmers amuse themselves by submitting answers to PP&CG.
如果程序员在工作中提交了包含高尔夫风格代码的拉取请求,它将被拒绝。他们的同事会嘲笑他们。他们的经理会到他们的办公桌旁聊天。即便如此,程序员还是通过向 PP&CG 提交答案来娱乐自己。
What does this have to do with stdc++.h
? As others have pointed out, using it is lazy. It's non-portable, so you don't know if it will work on your compiler or the next version of your compiler. It fosters bad habits. It's non-standard, so your program's behavior may differ from what you expect. It may increase compile time and executable size.
这有什么关系stdc++.h
?正如其他人指出的那样,使用它是懒惰的。它是不可移植的,因此您不知道它是否适用于您的编译器或编译器的下一个版本。它会培养坏习惯。它是非标准的,因此您的程序的行为可能与您期望的不同。它可能会增加编译时间和可执行文件的大小。
These are all valid and correct objections. So why would anyone use this monstrosity?
这些都是有效和正确的反对意见。那么为什么会有人使用这个怪物呢?
It turns out that some people like programming puzzleswithout the code golf. They get together and compete at events like ACM-ICPC, Google Code Jam, and Facebook Hacker Cup, or on sites like Topcoder and Codeforces. Their rank is based on program correctness, execution speed, and how fast they submit a solution. To maximize execution speed, many participants use C++. To maximize coding speed, some of them use stdc++.h
.
事实证明,有些人喜欢没有代码的编程谜题高尔夫。他们聚在一起参加 ACM-ICPC、Google Code Jam 和 Facebook Hacker Cup 等活动,或在 Topcoder 和 Codeforces 等网站上进行竞争。他们的排名基于程序的正确性、执行速度以及他们提交解决方案的速度。为了最大限度地提高执行速度,许多参与者使用 C++。为了最大限度地提高编码速度,其中一些使用stdc++.h
.
Is this is a good idea? Let's check the list of disadvantages. Portability? It doesn't matter since these coding events use a specific compiler version that contestants know in advance. Standards compliance? Not relevant for a block of code whose useful life is less than one hour. Compile time and executable size? These aren't part of the contest's scoring rubric.
这是个好主意吗?让我们检查一下缺点列表。可移植性?这并不重要,因为这些编码活动使用参赛者事先知道的特定编译器版本。符合标准?与使用寿命少于一小时的代码块无关。编译时间和可执行文件大小?这些不是比赛评分标准的一部分。
So we're left with bad habits. This is a valid objection. By using this header file, contestants are avoiding the chance to learn which standard header file defines the functionality they're using in their program. When they're writing real-world code (and not using stdc++.h
) they'll have to spend time looking up this information, which means they'll be less productive. That's the downside of practicing with stdc++.h
.
所以我们留下了坏习惯。这是一个有效的反对意见。通过使用这个头文件,参赛者避免了了解哪个标准头文件定义了他们在他们的程序中使用的功能的机会。当他们编写真实世界的代码(而不是使用stdc++.h
)时,他们将不得不花时间查找这些信息,这意味着他们的工作效率会降低。这就是使用stdc++.h
.
This raises the question of why it's worth taking part in competitive programming at all if it encourages bad habits like using stdc++.h
and violating other coding standards. One answer is that people do it for the same reason they post programs on PP&CG: some programmers find it enjoyable to use their coding skills in a game-like context.
这就提出了一个问题:如果竞争性编程鼓励使用stdc++.h
和违反其他编码标准等坏习惯,为什么值得参与竞争性编程。一个答案是人们这样做的原因与他们在 PP&CG 上发布程序的原因相同:一些程序员发现在类似游戏的环境中使用他们的编码技能很有趣。
So the question of whether to use stdc++.h
comes down to whether the coding speed benefits in a programming contest outweigh the bad habits that one might develop by using it.
因此,是否使用的问题stdc++.h
归结为在编程竞赛中编码速度的好处是否大于使用它可能养成的坏习惯。
This question asks: "Why should I not #include <bits/stdc++.h>
?" I realize that it was asked and answered to make a point, and the accepted answer is intended to be the One True Answer to this question. But the question isn't "Why should I not #include <bits/stdc++.h>
in production code?" Therefore, I think it's reasonable to consider other scenarios where the answer may be different.
这个问题问:“为什么我不应该 #include <bits/stdc++.h>
?” 我意识到有人问并回答是为了表明观点,接受的答案旨在成为这个问题的唯一正确答案。但问题不是“为什么我不应该<bits/stdc++.h>
在生产代码中#include ?” 因此,我认为考虑其他答案可能不同的场景是合理的。
回答by Bulletmagnet
From N4606, Working Draft, Standard for Programming Language C++ :
来自 N4606,工作草案,编程语言 C++ 标准:
17.6.1.2 Headers [headers]
17.6.1.2 头文件 [headers]
Each element of the C++ standard library is declared or defined (as appropriate) in a header.
The C++ standard library provides 61 C++ library headers, as shown in Table 14.
C++ 标准库的每个元素都在头文件中声明或定义(视情况而定)。
C++ 标准库提供了 61 个 C++ 库头文件,如表 14 所示。
Table 14 — C++ library headers
表 14 — C++ 库头文件
<algorithm> <future> <numeric> <strstream>
<any> <initializer_list> <optional> <system_error>
<array> <iomanip> <ostream> <thread>
<atomic> <ios> <queue> <tuple>
<bitset> <iosfwd> <random> <type_traits>
<chrono> <iostream> <ratio> <typeindex>
<codecvt> <istream> <regex> <typeinfo>
<complex> <iterator> <scoped_allocator> <unordered_map>
<condition_variable> <limits> <set> <unordered_set>
<deque> <list> <shared_mutex> <utility>
<exception> <locale> <sstream> <valarray>
<execution> <map> <stack> <variant>
<filesystem> <memory> <stdexcept> <vector>
<forward_list> <memory_resorce> <streambuf>
<fstream> <mutex> <string>
<functional> <new> <string_view>
There's no <bits/stdc++.h> there. This is not surprising, since <bits/...> headers are implementation detail, and usually carry a warning:
那里没有 <bits/stdc++.h> 。这并不奇怪,因为 <bits/...> 标头是实现细节,通常带有警告:
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly.
<bits/stdc++.h> also carries a warning:
<bits/stdc++.h> 还带有警告:
* This is an implementation file for a precompiled header.