日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

超详细!关于万能头文件<bits/stdc++.h>的细节

發(fā)布時間:2024/3/26 c/c++ 97 豆豆
生活随笔 收集整理的這篇文章主要介紹了 超详细!关于万能头文件<bits/stdc++.h>的细节 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

萬能頭文件引言

相信大家在C/C++中一定也遇到過這些情況:

  • 使用系統(tǒng)庫函數(shù)(如C++<cmath>庫,C<math.h>庫的開方函數(shù)double sqrt(double))和C++類(如array類,vector類)之后,發(fā)現(xiàn)編譯器報錯,到開頭補加頭文件:

  • 未定義標識符"string"

    未定義標識符"cout"

    后面有“::”的名稱一定是類名或命名空間名……

    (C++11之后<string>已經間接嵌入到C++輸入輸出流<iostream>之中了,但是平時使用的時候記得加上#include <string>)

    必須到開頭補加:

    #include <iostream> #include <string> #include <cmath> //C++繼承C //#include <math.h> C
  • 忘記函數(shù)是哪個頭文件,函數(shù)太多,對應的頭文件容易記混,而且頭文件名不好記憶。

  • 這里就有一個解決辦法了,以一招破萬招的辦法:

    用一個包含所有頭文件的頭文件,這里就是常用的<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++特有的類庫和迭代器庫等……已經可以滿足絕大多數(shù)需求。

    但是:<bits/stdc++.h>不屬于C/C++標準庫,不具有系統(tǒng)移植性,在Visual Studio項目里找不到頭文件。

    萬能頭文件的優(yōu)缺點

    優(yōu)點:

  • 可以減少了編寫所有必要頭文件的工作量

  • 對于使用的每個函數(shù),不用記住GNU C++的所有STL

  • 缺點:

  • 使用它將包含許多不必要的東西,并增加編譯時間

  • 這個頭文件不是C++標準的一部分,因此是不可移植的,應該避免

  • 編譯器每次編譯翻譯單元時都必須實際讀取和分析每個包含的頭文件,應該減少這類頭文件的使用

  • 創(chuàng)建萬能頭文件

  • 找到C盤C++配置環(huán)境目錄下的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項目里面打開系統(tǒng)頭文件所在位置,以<iostream>為例,右鍵打開"iostream"關鍵字選項,打開下拉菜單

  • 點擊:轉到文檔<iostream>,里面顯示的是<iostream>里面的定義內容,之后右鍵打開彈出的<iostream>文件選項,打開下拉菜單

    點擊:打開所在的文件夾,文件夾里面是visual studio此項目可以包含的一系列的系統(tǒng)頭文件

    直接粘貼bits文件夾到此目錄上

  • 或者自己創(chuàng)建一個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創(chuàng)建一個C/C++文件,導入#include <bits/stdc++.h>

  • 編譯器不會找不到此文件,而且沒有顯示任何語法錯誤

    注意要點

  • <bits/stdc++/h>并不能包括所有C/C++頭文件,例如C++20新增的<numbers>并不包括在萬能頭文件里面,還需另行導入。

  • 若要使用數(shù)學常量,可以另外導入<numbers>頭文件,訪問numbers類里面的內聯(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>)里面的宏定義,此時不需要再導入頭文件,但要在#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>有關數(shù)學常量定義比<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之后的標準會報錯,系統(tǒng)默認C++14標準,這里小編使用的是C++最新標準,也就是C++20之后標準,也會出現(xiàn)此問題

  • 在#inlcude <bits/stdc++.h>前面加上預處理命令#define _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS

  • #define _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS #include <bits/stdc++.h> ...

    這樣編譯器就不會報錯了

    總結

    以上是生活随笔為你收集整理的超详细!关于万能头文件<bits/stdc++.h>的细节的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。