17
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#include<bits/stdc++.h> せずに標準ライブラリヘッダを全て include する🤔

Last updated at Posted at 2022-03-06

この記事は ICPC OB/OG の会(JAG)による 2021年度模擬地区予選問題の一部ネタバレが含まれます。
「ICPC? なにそれ」な人は気にせずにお読み下さい。
これから2021年度JAG模擬地区を解こうと考えている方は、解法自体には触れませんが、お気をつけ下さい。

† #include<bits/stdc++.h> †

 皆さんはC/C++のソースコードを読んでいて、 #include<bits/stdc++.h> と書かれたソースコードに出会ったことはあるでしょうか?
 これはコンパイラにGCCを使用していると使える、すべての標準ライブラリを一度にインクルードできるヘッダファイルです。
 主に競技プログラミング界隈、特にICPCや情報オリンピック本選など、ソースコードの準備が許されず、コンテスト時間内に全ての解答を1から書かないといけない場所で重宝されます。詳しい説明は以下の記事を参照して下さい。

この様に特定の用途では便利ですが、GCC を使っていないと使えない、非標準機能 なので、例えば Visual Studio や clang では使用できません。Mac の gcc も、デフォルトのものは中身は clang なので使用できません。
普段は gcc を使用しているけど、提出してみると何かがおかしい・clang の方が高速に実行されるかもとなり clang で提出することもあるかもしれません。そのとき、bits/stdc++.h を使っていると無慈悲に CE と言われてしまいます。

#if __has_include(<bits/stdc++.h>)
#include <bits/stdc++.h>
#endif  // bits/stdc++

と書けば <bits/stdc++.h> がある場合のみ include されることになりclang でも安心ですが、やはり include 漏れしたときにどのヘッダをincludeしないといけないか調べる時間ロスが発生してしまいます。

include の仕組み

C/C++ のincludeは、基本的に他のファイルをそこに展開するだけであるので、例えばヘッダAがヘッダBをincludeしているとき、ヘッダAをincludeさえすればヘッダBもincludeされます。
例えば <map> を include すれば、std::map ではstd::pair も使用されているため、<utility>ヘッダもincludeされることが多いです。

※ この例では std::pair が使えればいいだけで、言語仕様的には <utility>ヘッダを include されるとは明示されているわけではありません。例えば <internal/pair_impl> に実装が書かれていて、そちらが include される可能性はあります。言語仕様で示されている例であれば、<utility><initializer_list> を include される、などがあります。

というわけで非標準である bits/stdc++.h を避けつつ、標準ライブラリヘッダを全て include するために、最小で何個の標準ライブラリヘッダのincludeが必要かという話をします。

bits/stdc++.h の中身を確認する

GCC が手元にあれば bits/stdc++.h を探して直接見てみても良いですし、ネット上 でも確認することができます。
GCC や clang を使っている場合、コンパイルオプションに -H を追加すると include しているヘッダが全て表示されるので、#include<bits/stdc++.h> が含まれるコードを -H 付きでコンパイルすることでも場所を確認できます。
GCC9.3.0 を入れた Ubuntu では /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h にありました。

GCCのバージョンや、C か C++か、また言語バージョンにもよって何が include されるかは微妙に変わりますが、GCC9.3.0 の C++17 では以下の88ヘッダが include されるようです。
cassert, cctype, cerrno, cfloat, ciso646, climits, clocale, cmath, csetjmp, csignal, cstdarg, cstddef, cstdio, cstdlib, cstring, ctime, cwchar, cwctype, ccomplex, cfenv, cinttypes, cstdalign, cstdbool, cstdint, ctgmath, cuchar, algorithm, bitset, complex, deque, exception, fstream, functional, iomanip, ios, iosfwd, iostream, istream, iterator, limits, list, locale, map, memory, new, numeric, ostream, queue, set, sstream, stack, stdexcept, streambuf, string, typeinfo, utility, valarray, vector, array, atomic, chrono, codecvt, condition_variable, forward_list, future, initializer_list, mutex, random, ratio, regex, scoped_allocator, system_error, thread, tuple, typeindex, type_traits, unordered_map, unordered_set, shared_mutex, any, charconv, filesystem, optional, memory_resource, string_view, variant, bit, version

cstdio 等のように c から始まる C言語時代からのヘッダも含まれますが、とりあえずこれらをこの記事では標準ライブラリヘッダと呼ぶことにします。

それぞれが何を include するか確かめる

これらの標準ライブラリヘッダが、中身で何を include するか確かめます。今回はこれらを1つずつincludeするソースコードを生成して、-H で実際にincludeされるヘッダを確認します。
C++の仕様書で、~は~を include すると明示的に記述されているものもあり、そちらを基準にするべきかもしれませんが、明示されてない include 関係も多くあるので、今回の方式にしました。

Clang 10.0.0 の C++17 では、この様になりました(長いので折りたたみ)
cctype 0


chrono 8
 climits cstddef cstdint ctime limits ratio type_traits version

ctime 0


vector 22
 algorithm atomic bit climits cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

iomanip 41
 algorithm atomic bit bitset cctype cerrno chrono climits cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list ios iosfwd istream iterator limits locale memory mutex new ostream ratio stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility version

cstdint 0


future 35
 algorithm atomic bit cctype cerrno chrono climits cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list iosfwd iterator limits memory mutex new ratio stdexcept string string_view system_error thread tuple type_traits typeinfo utility version

locale 37
 algorithm atomic bit cctype cerrno chrono climits cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list ios iosfwd iterator limits memory mutex new ratio stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility version

string 26
 algorithm atomic bit cctype cstddef cstdint cstdio cstdlib cstring cwchar cwctype exception functional initializer_list iosfwd iterator limits memory new stdexcept string_view tuple type_traits typeinfo utility version

random 44
 algorithm atomic bit bitset cctype cerrno chrono climits cmath cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list ios iosfwd istream iterator limits locale memory mutex new numeric ostream ratio stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility vector version

condition_variable 33
 algorithm atomic bit cctype cerrno chrono climits cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list iosfwd iterator limits memory new ratio stdexcept string string_view system_error tuple type_traits typeinfo utility version

cstddef 2
 type_traits version

memory 17
 atomic cstddef cstdint cstdlib cstring exception initializer_list iosfwd iterator limits new stdexcept tuple type_traits typeinfo utility version

functional 18
 atomic cstddef cstdint cstdlib cstring exception initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

cwctype 1
 cctype

streambuf 35
 algorithm atomic bit cctype cerrno chrono climits cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list ios iosfwd iterator limits memory mutex new ratio stdexcept string string_view system_error tuple type_traits typeinfo utility version

cstring 0


cassert 0


numeric 20
 atomic cmath cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

typeindex 13
 cstddef cstdint cstdlib cstring exception initializer_list iosfwd limits new type_traits typeinfo utility version

cmath 4
 cstddef limits type_traits version

unordered_map 23
 algorithm atomic bit cmath cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new optional stdexcept tuple type_traits typeinfo utility version

stack 22
 algorithm atomic bit cstddef cstdint cstdlib cstring deque exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

cfloat 0


bit 5
 cstddef iosfwd limits type_traits version

array 21
 algorithm atomic bit cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

utility 7
 cstddef cstdint cstring initializer_list iosfwd type_traits version

scoped_allocator 18
 atomic cstddef cstdint cstdlib cstring exception initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

type_traits 2
 cstddef version

atomic 4
 cstddef cstdint type_traits version

codecvt 34
 algorithm atomic bit cctype cerrno chrono climits cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list iosfwd iterator limits memory mutex new ratio stdexcept string string_view system_error tuple type_traits typeinfo utility version

version 0


forward_list 21
 algorithm atomic bit cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

thread 33
 algorithm atomic bit cctype cerrno chrono climits cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list iosfwd iterator limits memory new ratio stdexcept string string_view system_error tuple type_traits typeinfo utility version

csetjmp 0


ctgmath 45
 algorithm atomic bit bitset ccomplex cctype cerrno chrono climits cmath complex cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list ios iosfwd istream iterator limits locale memory mutex new ostream ratio sstream stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility version

stdexcept 7
 cstddef cstdlib exception iosfwd limits type_traits version

cstdio 0


set 22
 algorithm atomic bit cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new optional stdexcept tuple type_traits typeinfo utility version

mutex 33
 algorithm atomic bit cctype cerrno chrono climits cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list iosfwd iterator limits memory new ratio stdexcept string string_view system_error tuple type_traits typeinfo utility version

iostream 41
 algorithm atomic bit bitset cctype cerrno chrono climits cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list ios iosfwd istream iterator limits locale memory mutex new ostream ratio stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility version

filesystem 44
 algorithm atomic bit bitset cctype cerrno chrono climits cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype deque exception functional initializer_list iomanip ios iosfwd istream iterator limits locale memory mutex new ostream ratio stack stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility version

charconv 6
 cerrno cstddef iosfwd limits type_traits version

exception 5
 cstddef cstdlib limits type_traits version

unordered_set 23
 algorithm atomic bit cmath cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new optional stdexcept tuple type_traits typeinfo utility version

new 6
 cstddef cstdlib exception limits type_traits version

ostream 39
 algorithm atomic bit bitset cctype cerrno chrono climits cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list ios iosfwd iterator limits locale memory mutex new ratio stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility version

variant 22
 algorithm array atomic bit cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

climits 0


cstdlib 4
 cstddef limits type_traits version

cstdarg 0


iosfwd 0


cwchar 2
 cctype cwctype

cerrno 0


cinttypes 1
 cstdint

deque 21
 algorithm atomic bit cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

sstream 41
 algorithm atomic bit bitset cctype cerrno chrono climits cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list ios iosfwd istream iterator limits locale memory mutex new ostream ratio stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility version

initializer_list 3
 cstddef type_traits version

bitset 28
 algorithm atomic bit cctype climits cstddef cstdint cstdio cstdlib cstring cwchar cwctype exception functional initializer_list iosfwd iterator limits memory new stdexcept string string_view tuple type_traits typeinfo utility version

typeinfo 7
 cstddef cstdint cstdlib exception limits type_traits version

tuple 13
 cstddef cstdint cstdlib cstring exception initializer_list iosfwd limits new type_traits typeinfo utility version

ciso646 0


any 18
 atomic cstddef cstdint cstdlib cstring exception initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

system_error 28
 algorithm atomic bit cctype cerrno cstddef cstdint cstdio cstdlib cstring cwchar cwctype exception functional initializer_list iosfwd iterator limits memory new stdexcept string string_view tuple type_traits typeinfo utility version

ccomplex 44
 algorithm atomic bit bitset cctype cerrno chrono climits cmath complex cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list ios iosfwd istream iterator limits locale memory mutex new ostream ratio sstream stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility version

map 22
 algorithm atomic bit cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new optional stdexcept tuple type_traits typeinfo utility version

shared_mutex 33
 algorithm atomic bit cctype cerrno chrono climits cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list iosfwd iterator limits memory new ratio stdexcept string string_view system_error tuple type_traits typeinfo utility version

csignal 0


limits 3
 cstddef type_traits version

ratio 5
 climits cstddef cstdint type_traits version

fstream 45
 algorithm atomic bit bitset cctype cerrno chrono climits cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype deque exception filesystem functional initializer_list iomanip ios iosfwd istream iterator limits locale memory mutex new ostream ratio stack stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility version

optional 19
 atomic cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

cfenv 0


ios 34
 algorithm atomic bit cctype cerrno chrono climits cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list iosfwd iterator limits memory mutex new ratio stdexcept string string_view system_error tuple type_traits typeinfo utility version

algorithm 20
 atomic bit cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

cstdbool 0


istream 40
 algorithm atomic bit bitset cctype cerrno chrono climits cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list ios iosfwd iterator limits locale memory mutex new ostream ratio stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility version

clocale 0


complex 43
 algorithm atomic bit bitset cctype cerrno chrono climits cmath cstdarg cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype exception functional initializer_list ios iosfwd istream iterator limits locale memory mutex new ostream ratio sstream stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility version

list 21
 algorithm atomic bit cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

iterator 13
 cstddef cstdint cstdlib cstring exception initializer_list iosfwd limits new type_traits typeinfo utility version

string_view 22
 algorithm atomic bit cstddef cstdint cstdio cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version

regex 36
 algorithm atomic bit cctype cerrno chrono climits cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype deque exception functional initializer_list iosfwd iterator limits memory mutex new ratio stdexcept string string_view system_error tuple type_traits typeinfo utility vector version

queue 24
 algorithm atomic bit climits cstddef cstdint cstdlib cstring deque exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility vector version

valarray 22
 algorithm atomic bit cmath cstddef cstdint cstdlib cstring exception functional initializer_list iosfwd iterator limits memory new stdexcept tuple type_traits typeinfo utility version
また GCC9.3.0のC++17でも試したら、この様になりました(やっぱり長いので折りたたみ)
cctype 0


chrono 5
 cstdint ctime limits ratio type_traits

ctime 0


vector 6
 exception initializer_list new type_traits typeinfo utility

iomanip 29
 array cctype cerrno clocale cstdint cstdio cstdlib ctime cwchar cwctype exception initializer_list ios iosfwd istream limits locale new ostream sstream stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility

cstdint 0


future 29
 array atomic cctype cerrno chrono clocale condition_variable cstdint cstdio cstdlib ctime cwchar exception initializer_list iosfwd limits memory mutex new ratio stdexcept string string_view system_error thread tuple type_traits typeinfo utility

locale 24
 array cctype cerrno clocale cstdint cstdio cstdlib ctime cwchar cwctype exception initializer_list iosfwd limits new stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility

string 15
 cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new string_view type_traits typeinfo

random 20
 cctype cerrno clocale cmath cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new numeric string string_view type_traits typeinfo utility vector

condition_variable 24
 array cctype cerrno chrono clocale cstdint cstdio cstdlib ctime cwchar exception initializer_list iosfwd limits new ratio stdexcept string string_view system_error tuple type_traits typeinfo utility

cstddef 0


memory 20
 array cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new stdexcept string string_view tuple type_traits typeinfo utility

functional 23
 array cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new optional stdexcept string string_view tuple type_traits typeinfo unordered_map utility vector

cwctype 0


streambuf 18
 cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new stdexcept string string_view system_error type_traits typeinfo

cstring 0


cassert 0


numeric 1
 type_traits

typeindex 1
 typeinfo

cmath 2
 limits type_traits

unordered_map 21
 array cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new optional stdexcept string string_view tuple type_traits typeinfo utility

stack 7
 deque exception initializer_list new type_traits typeinfo utility

cfloat 0


bit 2
 limits type_traits

array 18
 cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new stdexcept string string_view type_traits typeinfo utility

utility 2
 initializer_list type_traits

scoped_allocator 21
 array cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits memory new stdexcept string string_view tuple type_traits typeinfo utility

type_traits 0


atomic 1
 type_traits

codecvt 16
 cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new string string_view type_traits typeinfo

version 0


forward_list 5
 exception initializer_list new type_traits typeinfo

thread 24
 array cctype cerrno chrono clocale cstdint cstdio cstdlib ctime cwchar exception initializer_list iosfwd limits memory new ratio stdexcept string string_view tuple type_traits typeinfo utility

csetjmp 0


ctgmath 26
 cctype cerrno clocale cmath complex cstdint cstdio cstdlib cwchar cwctype exception initializer_list ios iosfwd istream limits new ostream sstream stdexcept streambuf string string_view system_error type_traits typeinfo

stdexcept 16
 cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new string string_view type_traits typeinfo

cstdio 0


set 19
 cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new optional stdexcept string string_view type_traits typeinfo utility

mutex 24
 array cctype cerrno chrono clocale cstdint cstdio cstdlib ctime cwchar exception initializer_list iosfwd limits new ratio stdexcept string string_view system_error tuple type_traits typeinfo utility

iostream 23
 cctype cerrno clocale cstdint cstdio cstdlib cwchar cwctype exception initializer_list ios iosfwd istream limits new ostream stdexcept streambuf string string_view system_error type_traits typeinfo

filesystem 33
 array cctype cerrno chrono clocale codecvt cstdint cstdio cstdlib ctime cwchar cwctype exception initializer_list iomanip ios iosfwd istream limits locale new ostream ratio sstream stdexcept streambuf string string_view system_error tuple type_traits typeinfo utility

charconv 4
 cctype cerrno limits type_traits

exception 3
 new type_traits typeinfo

unordered_set 21
 array cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new optional stdexcept string string_view tuple type_traits typeinfo utility

new 3
 exception type_traits typeinfo

ostream 21
 cctype cerrno clocale cstdint cstdio cstdlib cwchar cwctype exception initializer_list ios iosfwd limits new stdexcept streambuf string string_view system_error type_traits typeinfo

variant 7
 exception initializer_list limits new type_traits typeinfo utility

memory_resource 29
 array cctype cerrno chrono clocale condition_variable cstddef cstdint cstdio cstdlib ctime cwchar exception initializer_list iosfwd limits memory new ratio shared_mutex stdexcept string string_view system_error tuple type_traits typeinfo utility vector

climits 0


cstdlib 0


cstdarg 0


iosfwd 1
 cwchar

cwchar 0


cerrno 0


cinttypes 1
 cstdint

deque 6
 exception initializer_list new type_traits typeinfo utility

sstream 23
 cctype cerrno clocale cstdint cstdio cstdlib cwchar cwctype exception initializer_list ios iosfwd istream limits new ostream stdexcept streambuf string string_view system_error type_traits typeinfo

initializer_list 0


bitset 16
 cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new string string_view type_traits typeinfo

typeinfo 0


tuple 19
 array cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new stdexcept string string_view type_traits typeinfo utility

ciso646 0


any 6
 exception initializer_list new type_traits typeinfo utility

system_error 17
 cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new stdexcept string string_view type_traits typeinfo

ccomplex 26
 cctype cerrno clocale cmath complex cstdint cstdio cstdlib cwchar cwctype exception initializer_list ios iosfwd istream limits new ostream sstream stdexcept streambuf string string_view system_error type_traits typeinfo

map 21
 array cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new optional stdexcept string string_view tuple type_traits typeinfo utility

shared_mutex 25
 array cctype cerrno chrono clocale condition_variable cstdint cstdio cstdlib ctime cwchar exception initializer_list iosfwd limits new ratio stdexcept string string_view system_error tuple type_traits typeinfo utility

csignal 0


limits 0


cstdalign 0


ratio 2
 cstdint type_traits

fstream 23
 cctype cerrno clocale cstdint cstdio cstdlib cwchar cwctype exception initializer_list ios iosfwd istream limits new ostream stdexcept streambuf string string_view system_error type_traits typeinfo

optional 18
 cctype cerrno clocale cstdint cstdio cstdlib cwchar exception initializer_list iosfwd limits new stdexcept string string_view type_traits typeinfo utility

cfenv 0


ios 20
 cctype cerrno clocale cstdint cstdio cstdlib cwchar cwctype exception initializer_list iosfwd limits new stdexcept streambuf string string_view system_error type_traits typeinfo

algorithm 24
 array cctype cerrno clocale cstdint cstdio cstdlib cwchar exception functional initializer_list iosfwd limits new optional stdexcept string string_view tuple type_traits typeinfo unordered_map utility vector

cstdbool 0


istream 22
 cctype cerrno clocale cstdint cstdio cstdlib cwchar cwctype exception initializer_list ios iosfwd limits new ostream stdexcept streambuf string string_view system_error type_traits typeinfo

clocale 0


complex 25
 cctype cerrno clocale cmath cstdint cstdio cstdlib cwchar cwctype exception initializer_list ios iosfwd istream limits new ostream sstream stdexcept streambuf string string_view system_error type_traits typeinfo

list 5
 exception initializer_list new type_traits typeinfo

cuchar 1
 cwchar

iterator 23
 cctype cerrno clocale cstdint cstdio cstdlib cwchar cwctype exception initializer_list ios iosfwd istream limits new ostream stdexcept streambuf string string_view system_error type_traits typeinfo

string_view 6
 cstdint cwchar initializer_list iosfwd limits type_traits

regex 47
 algorithm array bitset cctype cerrno chrono clocale condition_variable cstddef cstdint cstdio cstdlib cstring ctime cwchar cwctype deque exception functional initializer_list ios iosfwd istream iterator limits locale map memory memory_resource new optional ostream ratio shared_mutex sstream stack stdexcept streambuf string string_view system_error tuple type_traits typeinfo unordered_map utility vector

queue 8
 deque exception initializer_list new type_traits typeinfo utility vector

valarray 26
 algorithm array cctype cerrno clocale cmath cstdint cstdio cstdlib cwchar exception functional initializer_list iosfwd limits new optional stdexcept string string_view tuple type_traits typeinfo unordered_map utility vector

標準ライブラリヘッダを全て include するのに必要な、最低限のヘッダを求める

どうやって求めるべきかは、この競プロの問題を参考にします。このH問題「include」が該当する問題です。
JAG 2021年度模擬地区予選問題 解説
解くにあたって、競プロなどをしていないと知らないようなテクニックが使用されますが、参加チームのほぼ全てのチームが解いているので、ICPC アジア地区に参加するレベルの参加者では皆知っているようなものです[要出典]。
(競技プログラミングは標準ライブラリヘッダを最低限の数のincludeで済ませたいときに役に立つ!🤔)

これを元に求めると、clang 10.0.0 の C++17 では、以下の31ヘッダを include することで、標準ライブラリヘッダを全て include することができることが分かりました。

[00] cassert
[00] cfenv
[00] cfloat
[00] ciso646
[00] clocale
[00] csetjmp
[00] csignal
[00] cstdbool
[01] cinttypes
[06] charconv
[13] typeindex
[18] any
[18] scoped_allocator
[21] forward_list
[21] list
[22] map
[22] set
[22] valarray
[22] variant
[23] unordered_map
[23] unordered_set
[24] queue
[33] condition_variable
[33] shared_mutex
[34] codecvt
[35] future
[36] regex
[41] iostream
[44] random
[45] ctgmath
[45] fstream

先頭の [nn] は、そのヘッダが何個の標準ライブラリヘッダをincludeしているのかを表しています。fstream は45個と、半分以上の標準ライブラリヘッダをincludeしているようです。

同様に GCC9.3.0 の C++17 でも試したら、以下のようになりました

[00] cassert
[00] cfenv
[00] cfloat
[00] ciso646
[00] climits
[00] csetjmp
[00] csignal
[00] cstdalign
[00] cstdarg
[00] cstdbool
[00] version
[01] cinttypes
[01] cuchar
[01] typeindex
[02] bit
[04] charconv
[05] forward_list
[05] list
[06] any
[07] variant
[08] queue
[19] set
[20] random
[21] scoped_allocator
[21] unordered_set
[23] fstream
[23] iostream
[26] ccomplex
[26] ctgmath
[26] valarray
[29] future
[33] filesystem
[47] regex

regex を include するだけで、実に47ヘッダもincludeされます。凄いですね…。
各コンパイラ・言語バージョンで試してみた結果は以下のとおりです。

Clang 10.0.0 / C++14
[00] cassert
[00] cfenv
[00] cfloat
[00] ciso646
[00] clocale
[00] csetjmp
[00] csignal
[00] cstdbool
[01] cinttypes
[12] typeindex
[17] scoped_allocator
[19] array
[19] forward_list
[19] list
[19] map
[19] set
[20] unordered_map
[20] unordered_set
[20] valarray
[22] queue
[30] condition_variable
[31] codecvt
[32] future
[33] regex
[38] iostream
[41] fstream
[41] random
[42] ctgmath
GCC 9.3.0 / C++14
[00] cassert
[00] cfenv
[00] cfloat
[00] ciso646
[00] climits
[00] csetjmp
[00] csignal
[00] cstdalign
[00] cstdarg
[00] cstdbool
[00] cstddef
[01] cinttypes
[01] cuchar
[01] typeindex
[05] forward_list
[05] list
[05] set
[07] queue
[10] valarray
[14] codecvt
[18] functional
[18] random
[19] scoped_allocator
[19] unordered_map
[19] unordered_set
[21] fstream
[21] iostream
[24] ccomplex
[24] ctgmath
[27] iomanip
[28] future
[37] regex
GCC 9.3.0 / C++14
[00] cassert
[00] cfloat
[00] ciso646
[00] climits
[00] csetjmp
[00] csignal
[00] cstdarg
[00] cstddef
[00] cstring
[00] functional
[00] limits
[00] numeric
[00] typeinfo
[02] list
[02] map
[02] memory
[02] set
[03] queue
[03] stack
[06] valarray
[07] bitset
[08] iomanip
[11] locale
[13] iostream
[13] iterator
[15] complex
[15] fstream

終わりに

いかがだったでしょうか?
<bits/stdc++.h> が無い環境用に88個のヘッダを書かなくても、30個程度のヘッダを書けば良さそうということが分かりました。ぜひご活用ください!
良ければ記事の LGTM・ストックお願いします!

え、競技プログラマはテンプレヘッダの長さを気にしない…?

17
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?