超详细!关于万能头文件<bits/stdc++.h>的细节
万能头文件引言
相信大家在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>
...
这样编译器就不会报错了
超详细!关于万能头文件<bits/stdc++.h>的细节相关推荐
- C++万能头文件(bits/stdc++.h)
先说一句 C++万能头文件,并不是所有场合都能用,比如说POJ. 我曾在POJ提交过程序,编译错误.就是因为用了万能头文件. 意思就是,不认识<bits/stdc++.h>. 因为,万能头 ...
- VS C++万能头文件bits/stdc++.h的配置
万能头文件:#include<bits/stdc++.h> 路径:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\To ...
- 【环境配置】macOS的Xcode中使用C++万能头文件bits/stdc++.h
启动终端 cd /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c ...
- 【C++】头文件 bits/stdc++.h 是啥?
原文地址: [C++]头文件 bits/stdc++.h 是啥? 欢迎访问我的博客:http://blog.duhbb.com/ 嘿嘿, 以后写 leetcode 的话, 本地直接就引用这个文件, 还 ...
- 浅说万能头<bits/stdc++.h>
#include<bits/stdc++.h>包含了C和C++的绝大多数头文件,像iostream.cmath.algorithm.iomanip.string之类的,这意味着我们不再需要 ...
- C++预编译头文件 bits/stdc++.h
有时候会看到别人包含这样的头文件: #include "bits/stdc++.h" 这个头文件中有很多预先包含的头文件,内容如下: // C++ includes used fo ...
- 高级c++头文件bits/stdc++.h
用这种方法声明头文件只需两行代码 #include<bits/stdc++.h> using namespace std; 这个头文件包含以下等等C++中包含的所有头文件: #includ ...
- Cfree之万能头函数bits\stdc++.h的添加和注意事项
实现步骤 一.创建一个.txt文件,把下面的代码复制进去 // C++ includes used for precompiling -*- C++ -*-// Copyright (C) 2003- ...
- std.h对应linux头文件,bits/stdc++.h头文件介绍(包含源代码)
注:转自http://blog.csdn.net/charles_dong2/article/details/56909347,同为本人写的,有部分修改. 之前在一个小OJ上刷题时发现有人是这么写的: ...
- VScode找不到C++万能头文件<bits/stdc++.h>解决办法
VScode找不到C++万能头文件<bits/stdc++.h>解决办法 一.万能头文件介绍 万能头文件<bits/stdc++> 中包含了 C++中大部分头文件,在大部分做题 ...
最新文章
- 服务器cpu天梯图_九月手机处理器排名 2020年9月最新版手机CPU天梯图
- 数据中心机房布线系统运维和管理
- 如何高效学Python?好用的爬虫工具又有哪些?
- DEBUG的参数说明
- STM32的I2C主从机通信
- 深入理解JavaScript中的this关键字
- Sybase数据库优化手册
- Android Exception 8(Couldn't read row 0, col -1 from CursorWindow)
- ice(Internet Communications Engine) window 安装与配置
- platform设备的添加
- Cubase Elements v11.0.0 WiN 23GB含音色库 中文完整版编曲录音软件
- 《FLUENT 14.0超级学习手册》——3.2 Gambit的应用
- SQL Server【获取当前时间】
- Macbook安装双系统的方法
- 什么是MySQL的预编译?
- [第一章 web入门]粗心的小李
- Final Cut Pro 导出视频教程「上」
- 转:程序员必读书单 1.0
- 用Python下载抖音无水印视频!
- bbm for android apk,BBM For Android App Screen Shots Leaked
热门文章
- PR不支持导入MKV【Influx插件】
- web网站安全 CSRF介绍及解决方案
- 基于智能矿山电力监控系统的设计与应用方法
- VBA文件对话框的应用(VBA打开文件、VBA选择文件、VBA选择文件夹)
- r75800h和r74800h的区别 r7 5800h和r7 4800h差多少
- rotate java 参数_java rotateLeft()和rotateRight()方法
- ChatGPT 革命性读书方法已被证明是一种非常有效和高效的学习和吸收新信息的方法
- 算法练习(5)———木块问题
- 一位程序员的独白:尽管我一生坎坷,但我仍被岁月折磨的死去活来
- springboot框架学习 图书管理系统的简单实现