简体   繁体   English

C++ STL:包括所有库?

[英]C++ STL : Including all libraries?

How to include all the libraries of stl in my c++ code rather than individually including them.如何在我的 c++ 代码中包含所有 stl 库,而不是单独包含它们。 For example -例如 -

#include <queue>
#include <containers> 

A simple way to include all of them.包含所有这些的简单方法。 Please help ?请帮忙 ?

You can't but you can just make a list and put them in a header with a header guard ofc.你不能,但你可以只列出一个列表并将它们放在一个带有标题保护的标题中。

// C++ Full Standard Header Include
#include <cstdlib>
#include <csignal>
#include <csetjmp>
#include <cstdarg>
#include <typeinfo>
#include <typeindex>
#include <type_traits>
#include <bitset>
#include <functional>
#include <utility>
#include <ctime>
#include <chrono>
#include <cstddef>
#include <initializer_list>
#include <tuple>
#include <new>
#include <memory>
#include <scoped_allocator>
#include <climits>
#include <cfloat>
#include <cstdint>
#include <cinttypes>
#include <limits>
#include <exception>
#include <stdexcept>
#include <cassert>
#include <system_error>
#include <cerrno>
#include <cctype>
#include <cwctype>
#include <cstring>
#include <cwstring>
#include <cwchar>
#include <cuchar>
#include <string>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <complex>
#include <valarray>
#include <random>
#include <numeric>
#include <ratio>
#include <cfenv>
#include <iosfwd>
#include <ios>
#include <istream>
#include <ostream>
#include <iostream>
#include <fstream>
#include <sstream>
#include <strstream>
#include <iomanip>
#include <streambuf>
#include <cstdio>
#include <locale>
#include <clocale>
#include <codecvt>
#include <regex>
#include <atomic>
#include <thread>
#include <mutex>
#include <future>
#include <condition_variable>
#include <ciso646>
#include <ccomplex>
#include <ctgmath>
#include <cstdalign>
#include <cstdbool>

That should be the full list :) ( C++11 has to be active too ) You might want to remove the ones that are deprecated ( shouldn't be many / you will get a warning anyway ) and the ones you don't have because they're special / old / new这应该是完整列表:)(C++11 也必须处于活动状态)您可能想要删除已弃用的那些(不应该很多/无论如何你都会收到警告)和那些你不推荐的因为它们很特别/旧/新

#include <bits/stdc++.h>

这个库包括大多数 STL 头文件(尤其是容器)

Preferably you should include only those header files you need because when preprocessor goes through your source file it would replace all header files with there actual contents.最好只包含您需要的那些头文件,因为当预处理器遍历您的源文件时,它将用实际内容替换所有头文件。 So , always try to minimize the use of headers in your program...因此,请始终尽量减少程序中标头的使用...

There is no way and no need to do it in most cases.在大多数情况下,没有办法也没有必要这样做。 If you have more than 1 c++ file you could create header file which includes everything what you need and include this single header in all your c++ files - this approach also helps to use precompiled headers.如果您有超过 1 个 c++ 文件,您可以创建包含您需要的所有内容的头文件,并将这个单一头文件包含在所有 c++ 文件中 - 这种方法还有助于使用预编译头文件。

libstdc++ documents a header file called stdc++.h which includes most of (not all) the standard headers based on the language dialect (for example, neither libstdc++ or libc++ seem to implement <cuchar> ). libstdc++ 记录了一个名为stdc++.h的头文件,其中包括大多数(不是全部)基于语言方言的标准头文件(例如,libstdc++ 或 libc++ 似乎都没有实现<cuchar> )。 The page gives instructions on how to create a precompiled header from this.该页面提供了有关如何从中创建预编译标头的说明。

As far as I know, libc++ doesn't have stdc++.h .据我所知, libc++ 没有stdc++.h

In Visual Studio C++, the canonical precompiled header is stdafx.h .在 Visual Studio C++ 中,规范的预编译头文件是stdafx.h You can take the contents of stdc++.h and copy/paste it over if you wish.如果您愿意,您可以获取stdc++.h的内容并复制/粘贴它。 You will have to modify the #ifdef directives which check the language dialect.您将不得不修改检查语言方言的#ifdef指令。 In MSVC, __cplusplus seems to defined to something useless like 199711 .在 MSVC 中, __cplusplus似乎被定义为像199711这样无用的东西。 _MSC_VER may be of use in determining the status of C++11 features. _MSC_VER可用于确定 C++11 功能的状态。

To use it on GCC, #include <bits/stdc++.h> .要在 GCC 上使用它, #include <bits/stdc++.h>

There isn't an easy, durable way to #include everything.没有一种简单、持久的方法来#include一切。

Even doing the obvious thing, as has been suggested , doesn't really work.即使像建议的那样做显而易见的事情,也没有真正起作用。 The STL is updated every three years--libraries occasionally get deprecated/removed, and new ones get added all the time--and the list given in that answer is already quite obsolete. STL 每三年更新一次——库偶尔会被弃用/删除,并且一直在添加新库——并且该答案中给出的列表已经过时了。 Furthermore, even if this could be easily done it's hard to think of a situation where it would improve your code.此外,即使这很容易做到,也很难想到可以改进代码的情况。 Including unneeded libraries bloats your executable size and, as I explain elsewhere , slows compilation time.包含不需要的库会使您的可执行文件变大,并且正如我在其他地方解释的那样,会减慢编译时间。 It also makes your code confusing and therefore harder to maintain.它还使您的代码混乱,因此更难维护。

Compile time, executable size, and maintainability are coding considerations that inexperienced programmers often overlook in the rush to write code as soon as possible.编译时间、可执行文件大小和可维护性是没有经验的程序员在急于尽快编写代码时经常忽略的编码考虑因素。 Inexperienced programmers are also sometimes intimidated by the C++ STL and don't know where to start.缺乏经验的程序员有时也会被 C++ STL 吓倒,不知道从哪里开始。 I don't want to make assumptions, but I suspect your question is motivated by a combination of these two things.我不想做出假设,但我怀疑您的问题是由这两件事共同引起的。

If that's the case, remember that learning your away around the STL--what it provides, what it doesn't, what its limitations are, how it's organized, etc--is an important and unavoidable step to mastering any language.如果是这种情况,请记住,了解 STL——它提供什么、不提供什么、它的限制是什么、它是如何组织的等等——是掌握任何语言的重要且不可避免的步骤。 Fortunately, there are lots of resources to help you.幸运的是,有很多资源可以帮助您。 The Core Guidelines and the ISOCPP FAQ are written by the language's developers (largely with new C++ users in mind) and detail the broad strokes of the language.核心指南ISOCPP 常见问题解答由该语言的开发人员(主要考虑到新的 C++ 用户)编写,并详细介绍了该语言的广泛内容。 There are many excellent books available, too: I particularly recommend Stroustrup's Programming: Principles and Practices Using C++ .也有许多优秀的书籍:我特别推荐 Stroustrup 的《编程:使用 C++ 的原则和实践》 If you have a narrower question (about, say, what STL functions can sort iterable containers), consult cppreference .如果您有一个更狭窄的问题(例如,关于哪些 STL 函数可以对可迭代容器进行排序),请咨询cppreference On sites like leetcode you can practice what you've learned on good programming problems.leetcode 之类的网站上,您可以在好的编程问题上练习您所学的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM