万能头文件引言
相信大家在C/C++中一定也遇到过这些情况:
使用系统库函数(如C++<cmath>库,C<math.h>库的开方函数double sqrt(double))和C++类(如array类,vector类)之后,发现编译器报错,到开头补加头文件:
未定义标识符"string"
未定义标识符"cout"
后面有“::”的名称一定是类名或命名空间名……
(C++11之后<string>已经间接嵌入到C++输入输出流<iostream>之中了,但是平时使用的时候记得加上#include <string>)
必须到开头补加:
#include <iostream>#include <string>#include <cmath> //C++继承C//#include <math.h> C
忘记函数是哪个头文件,函数太多,对应的头文件容易记混,而且头文件名不好记忆。
这里就有一个解决办法了,以一招破万招的办法:
用一个包含所有头文件的头文件,这里就是常用的<bits/stdc++.h>头文件,妈妈再也不用担心我没写头文件了。
万能头文件是什么
在一些oj(Online Judge)平台上,一些比赛(比如蓝桥杯)甚至一些在线编程平台上面,<bits/stdc++.h>都很常见。
图片取自手机APP:C++编译器
那么它里面的内容是什么呢?
以下为<bits/stdc++.h>内容:
// C++ includes used for precompiling -*- C++ -*-// Copyright (C) 2003-2018 Free Software Foundation, Inc.//// This file is part of the GNU ISO C++ Library. This library is free// software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the// Free Software Foundation; either version 3, or (at your option)// any later version.// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// Under Section 7 of GPL version 3, you are granted additional// permissions described in the GCC Runtime Library Exception, version// 3.1, as published by the Free Software Foundation.// You should have received a copy of the GNU General Public License and// a copy of the GCC Runtime Library Exception along with this program;// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see// <http://www.gnu.org/licenses/>./** @file stdc++.h * This is an implementation file for a precompiled header. */// 17.4.1.2 Headers// 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 <cuchar>#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 <codecvt>#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#if __cplusplus >= 201402L#include <shared_mutex>#endif#if __cplusplus >= 201703L#include <charconv>#include <filesystem>#endif
有关于C和C++一系列常用的头文件包括在里面,针对ANSI(American National Standards Institute)C/C++标准(C99,C11,C++11,C++14,以及最新版C++20),导入相应的头文件。
包括C++从C继承的改良库(以c开头的库名),C++特有的类库和迭代器库等……已经可以满足绝大多数需求。
但是:<bits/stdc++.h>不属于C/C++标准库,不具有系统移植性,在Visual Studio项目里找不到头文件。
万能头文件的优缺点
优点:
可以减少了编写所有必要头文件的工作量
对于使用的每个函数,不用记住GNU C++的所有STL
缺点:
使用它将包含许多不必要的东西,并增加编译时间
这个头文件不是C++标准的一部分,因此是不可移植的,应该避免
编译器每次编译翻译单元时都必须实际读取和分析每个包含的头文件,应该减少这类头文件的使用
创建万能头文件
找到C盘C++配置环境目录下的bits文件夹
点击此电脑,在C盘上检索关键字:bits,找到基本路径为:"C:\Program Files\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\x86_64-w64-mingw32\bits"
bits文件夹里面的内容为:
里面包含<stdc++.h>文件
或者直接检索关键字:stdc++.h,注意文件类型是C/C++ Header,不是快捷方式
返回上一级目录,就是bits文件夹
直接复制整个bits文件夹,注意是整个文件夹,不是单一的<stdc++.h>文件
在visual studio项目里面打开系统头文件所在位置,以<iostream>为例,右键打开"iostream"关键字选项,打开下拉菜单
点击:转到文档<iostream>,里面显示的是<iostream>里面的定义内容,之后右键打开弹出的<iostream>文件选项,打开下拉菜单
点击:打开所在的文件夹,文件夹里面是visual studio此项目可以包含的一系列的系统头文件
直接粘贴bits文件夹到此目录上
或者自己创建一个stdc++.h文件,内容为:
// C++ includes used for precompiling -*- C++ -*-// Copyright (C) 2003-2018 Free Software Foundation, Inc.//// This file is part of the GNU ISO C++ Library. This library is free// software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the// Free Software Foundation; either version 3, or (at your option)// any later version.// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// Under Section 7 of GPL version 3, you are granted additional// permissions described in the GCC Runtime Library Exception, version// 3.1, as published by the Free Software Foundation.// You should have received a copy of the GNU General Public License and// a copy of the GCC Runtime Library Exception along with this program;// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see// <http://www.gnu.org/licenses/>./** @file stdc++.h * This is an implementation file for a precompiled header. */// 17.4.1.2 Headers// 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 <cuchar>#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 <codecvt>#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#if __cplusplus >= 201402L#include <shared_mutex>#endif#if __cplusplus >= 201703L#include <charconv>#include <filesystem>#endif
直接移动到include文件夹里面。
检验是否导入成功,打开visual studio创建一个C/C++文件,导入#include <bits/stdc++.h>
编译器不会找不到此文件,而且没有显示任何语法错误
注意要点
<bits/stdc++/h>并不能包括所有C/C++头文件,例如C++20新增的<numbers>并不包括在万能头文件里面,还需另行导入。
若要使用数学常量,可以另外导入<numbers>头文件,访问numbers类里面的内联常量函数
#include <bits/stdc++.h>#include <numbers>using namespace std;int main(){ cout << numbers::pi << endl; cout << numbers::e << endl;}
或者使用<cmath>(C <math.h>)里面的宏定义,此时不需要再导入头文件,但要在#include <bits/stdc++.h>语句前加上#define _USE_MATH_DEFINES的预处理命令
#define _USE_MATH_DEFINES#include <bits/stdc++.h>using namespace std;int main(){ cout << M_PI << endl; cout << M_E << endl;}
<numbers>有关数学常量定义比<cmath>里面更加全面,推荐使用<numbers>头文件
bits文件夹导入了但是编译器还是显示找不到头文件。
打开已经导入的bits文件夹里面的<stdc++.h>(用visual studio),直接拖动文件到visual studio的快捷方式
再回到源文件就不会报错了,或者关闭visual studio重新打开
最新标准C++语法会显示不兼容而报错
编译器会报错显示,C++20标准已经将<iso646.h>(C库)移植到<iostream>里,在导入<bits/stdc++.h>时会因为重复导入库而报错
解决办法:
打开项目选项,点击C++属性
若使用C++14之后的标准会报错,系统默认C++14标准,这里小编使用的是C++最新标准,也就是C++20之后标准,也会出现此问题
在#inlcude <bits/stdc++.h>前面加上预处理命令#define _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS:
#define _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS#include <bits/stdc++.h>...
这样编译器就不会报错了