--------------------------------------------------------------------------
C++库全解:http://www.cplusplus.com/reference/
编程中经常由于头文件不全导致编译不通过,通过查找发现C/C++有一个万能库
1 | #include <bits/stdc++.h> |
它是基本是C++中支持的一个几乎万能的头文件,包含所有的可用到的C++库函数,如<istream>/<ostream>/<stack>/<queue>。这样做题的时候直接敲上它,岂不是很方便!!!
不过值得注意的是:目前POJ还不支持<bits/stdc++.h>(G++、C++都不支持)。HDU部分支持(G++支持,C++不支持)
下面是<bits/stdc++.h>的源代码(IDE为Dev-C++ 5.11),该头文件的详细内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | //C #ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #if __cplusplus >= 201103L #include <ccomplex> #include <cfenv> #include <cinttypes> #include <cstdalign> #include <cstdbool> #include <cstdint> #include <ctgmath> #include <cwchar> #include <cwctype> #endif // C++ #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <typeindex> #include <type_traits> #include <unordered_map> #include <unordered_set> #endif |
无论是编程还是笔试面试的时候,都可以极大地减少思考专用头文件的时间,但是从软件工程的角度出发,最好最小化包含头文件,如果包含了一些你可能不会用到的头文件,就会增加不必要的编译时间和程序大小。
下面是优缺点的分析
bits/stdc++的缺点
- bits/stdc++.h 不是GNU C++库的标准头文件,所以如果你在一些编译器(除了GCC)上编译你的代码,可能会失败,比如MSVC没有这个头文件。
- 使用它会包含很多不必要的东西,并且会增加编译时间
- 这个头文件不是C++标准的一部分,所以是不可移植的,应该尽量避免。
- 尽管标准中有一些通用的头文件,但还是应该避免使用它来代替特定的头文件,因为编译器在每次编译转换单元时都实际地读取并解析每个包含的头文件(包括递归包含的头文件)。
bits/stdc++的优点
- 在比赛中,使用这个文件是一个好主意,当你想减少时间浪费在做选择的时候;特别是当你的排名对时间很敏感的时候。
- 这还减少了编写所有必要头文件的所有杂务。
- 你不必为使用的每个函数都记住GNU c++的所有STL。