超详细!关于万能头文件<bits/stdc++.h>的细节
萬能頭文件引言
相信大家在C/C++中一定也遇到過這些情況:
使用系統(tǒng)庫函數(shù)(如C++<cmath>庫,C<math.h>庫的開方函數(shù)double sqrt(double))和C++類(如array類,vector類)之后,發(fā)現(xiàn)編譯器報錯,到開頭補加頭文件:
未定義標(biāo)識符"string"
未定義標(biāo)識符"cout"
后面有“::”的名稱一定是類名或命名空間名……
(C++11之后<string>已經(jīng)間接嵌入到C++輸入輸出流<iostream>之中了,但是平時使用的時候記得加上#include <string>)
必須到開頭補加:
#include <iostream> #include <string> #include <cmath> //C++繼承C //#include <math.h> C忘記函數(shù)是哪個頭文件,函數(shù)太多,對應(yīng)的頭文件容易記混,而且頭文件名不好記憶。
這里就有一個解決辦法了,以一招破萬招的辦法:
用一個包含所有頭文件的頭文件,這里就是常用的<bits/stdc++.h>頭文件,媽媽再也不用擔(dān)心我沒寫頭文件了。
萬能頭文件是什么
在一些oj(Online Judge)平臺上,一些比賽(比如藍(lán)橋杯)甚至一些在線編程平臺上面,<bits/stdc++.h>都很常見。
圖片取自手機(jī)APP:C++編譯器
那么它里面的內(nèi)容是什么呢?
以下為<bits/stdc++.h>內(nèi)容:
// 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有關(guān)于C和C++一系列常用的頭文件包括在里面,針對ANSI(American National Standards Institute)C/C++標(biāo)準(zhǔn)(C99,C11,C++11,C++14,以及最新版C++20),導(dǎo)入相應(yīng)的頭文件。
包括C++從C繼承的改良庫(以c開頭的庫名),C++特有的類庫和迭代器庫等……已經(jīng)可以滿足絕大多數(shù)需求。
但是:<bits/stdc++.h>不屬于C/C++標(biāo)準(zhǔn)庫,不具有系統(tǒng)移植性,在Visual Studio項目里找不到頭文件。
萬能頭文件的優(yōu)缺點
優(yōu)點:
可以減少了編寫所有必要頭文件的工作量
對于使用的每個函數(shù),不用記住GNU C++的所有STL
缺點:
使用它將包含許多不必要的東西,并增加編譯時間
這個頭文件不是C++標(biāo)準(zhǔn)的一部分,因此是不可移植的,應(yīng)該避免
編譯器每次編譯翻譯單元時都必須實際讀取和分析每個包含的頭文件,應(yīng)該減少這類頭文件的使用
創(chuàng)建萬能頭文件
找到C盤C++配置環(huán)境目錄下的bits文件夾
點擊此電腦,在C盤上檢索關(guān)鍵字:bits,找到基本路徑為:"C:\Program Files\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\x86_64-w64-mingw32\bits"
bits文件夾里面的內(nèi)容為:
里面包含<stdc++.h>文件
或者直接檢索關(guān)鍵字:stdc++.h,注意文件類型是C/C++ Header,不是快捷方式
返回上一級目錄,就是bits文件夾
直接復(fù)制整個bits文件夾,注意是整個文件夾,不是單一的<stdc++.h>文件
在visual studio項目里面打開系統(tǒng)頭文件所在位置,以<iostream>為例,右鍵打開"iostream"關(guān)鍵字選項,打開下拉菜單
點擊:轉(zhuǎn)到文檔<iostream>,里面顯示的是<iostream>里面的定義內(nèi)容,之后右鍵打開彈出的<iostream>文件選項,打開下拉菜單
點擊:打開所在的文件夾,文件夾里面是visual studio此項目可以包含的一系列的系統(tǒng)頭文件
直接粘貼bits文件夾到此目錄上
或者自己創(chuàng)建一個stdc++.h文件,內(nèi)容為:
直接移動到include文件夾里面。
檢驗是否導(dǎo)入成功,打開visual studio創(chuàng)建一個C/C++文件,導(dǎo)入#include <bits/stdc++.h>
編譯器不會找不到此文件,而且沒有顯示任何語法錯誤
注意要點
<bits/stdc++/h>并不能包括所有C/C++頭文件,例如C++20新增的<numbers>并不包括在萬能頭文件里面,還需另行導(dǎo)入。
若要使用數(shù)學(xué)常量,可以另外導(dǎo)入<numbers>頭文件,訪問numbers類里面的內(nèi)聯(lián)常量函數(shù)
#include <bits/stdc++.h> #include <numbers> using namespace std; int main() {cout << numbers::pi << endl;cout << numbers::e << endl; }或者使用<cmath>(C <math.h>)里面的宏定義,此時不需要再導(dǎo)入頭文件,但要在#include <bits/stdc++.h>語句前加上#define _USE_MATH_DEFINES的預(yù)處理命令
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; int main() {cout << M_PI << endl;cout << M_E << endl; }<numbers>有關(guān)數(shù)學(xué)常量定義比<cmath>里面更加全面,推薦使用<numbers>頭文件
bits文件夾導(dǎo)入了但是編譯器還是顯示找不到頭文件。
打開已經(jīng)導(dǎo)入的bits文件夾里面的<stdc++.h>(用visual studio),直接拖動文件到visual studio的快捷方式
再回到源文件就不會報錯了,或者關(guān)閉visual studio重新打開
最新標(biāo)準(zhǔn)C++語法會顯示不兼容而報錯
編譯器會報錯顯示,C++20標(biāo)準(zhǔn)已經(jīng)將<iso646.h>(C庫)移植到<iostream>里,在導(dǎo)入<bits/stdc++.h>時會因為重復(fù)導(dǎo)入庫而報錯
解決辦法:
打開項目選項,點擊C++屬性
若使用C++14之后的標(biāo)準(zhǔn)會報錯,系統(tǒng)默認(rèn)C++14標(biāo)準(zhǔn),這里小編使用的是C++最新標(biāo)準(zhǔn),也就是C++20之后標(biāo)準(zhǔn),也會出現(xiàn)此問題
在#inlcude <bits/stdc++.h>前面加上預(yù)處理命令#define _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS:
這樣編譯器就不會報錯了
總結(jié)
以上是生活随笔為你收集整理的超详细!关于万能头文件<bits/stdc++.h>的细节的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2018南航计算机考研分数线,2018-
- 下一篇: 基于人脸识别的门禁系统报告