7

I am working on a class project using vectors and linked lists. But in C++ in order to apply them I need to have the following code in my header.

#include<list>
#include<vector>

I know that both of these are a part of the standard template library. So I'd like to do a single

#include<StandardTemplateLibrary>

to save lines. But everywhere I look I don't see a singular command to add to my code and I've tried cstdlib, stdlib, cstdlib.h and none of them contain the keywords I need.

Is there a singular preprocessor that I can add to my project to do both of these? Or do I just have to include both? If you can refer me to source to read as well that'd be greatly appreciated.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Callat
  • 2,928
  • 5
  • 30
  • 47
  • 5
    Why is "saving lines" important? – Bart Friederichs Apr 21 '16 at 06:53
  • 1
    I don't think it's a good idea to `include` the whole library, most of which your code does not even use. – Dean Seo Apr 21 '16 at 06:55
  • 1
    To extend the previous comment: because it will slow down compilation significantly (of course depending on how fast your computer is). – Emil Laine Apr 21 '16 at 06:57
  • Related: [Why should I not #include ?](http://stackoverflow.com/q/31816095/3425536) – Emil Laine Apr 21 '16 at 07:04
  • No real reason @BartFriederichs it just seems like it'd be easier to call functions that I may need later if I just include the whole thing instead of including it in pieces. But I couldn't find `` anywhere I looked. Either I suck at research or I came to the right place. – Callat Apr 21 '16 at 07:14
  • StandardTemplateLibrary is certainly the wrong name. – Pete Becker Apr 21 '16 at 10:45
  • @PeteBecker I am most certainly aware. I included that to express the idea of what I'm trying to go for. This question gave me lots of feedback on how to use `#include` and that it is a compiler related header that shouldn't be used because it does add to compile time by alot by adding a ton of un-needed info. Saving keystrokes isn't the best way to program, its creating error free light complexity code. I learned quite a bit but for some reason I got 3 downvotes. :-( I don't know if I asked a bad question or if it's just hatred for `bits/stdc++.h` XD – Callat Apr 21 '16 at 12:50
  • @Hikari - my point was about the **meaning** of "StandardTemplateLibrary**, not its length. C++ has a **standard library**. It does not have a **standard template library**. STL was adopted as **part** of the standard library. It did not swallow it. – Pete Becker Apr 21 '16 at 18:28

8 Answers8

19

On some compilers, including <bits/stdc++.h> might do what you're looking for.

Note however that it makes your code nonportable (it may not work on other compilers, or even different versions of the same compiler). This is ok in some cases.

More info about why doing this might not be a good idea: Why should I not #include <bits/stdc++.h>?

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
7

You can use:

#include<bits/stdc++.h> 

as even suggested by everyone.But it is not a standard header file. The disadvantages of it are that it is

  • increases the compilation time.(As it includes all the header files together)
  • uses an internal non-standard header file of the GNU C++ library, and so will not compile in MSVC, XCode, and many other compilers
hithard
  • 123
  • 2
  • 8
5

Is there a singular preprocessor that I can add to my project to do both of these? Or do I just have to include both?

No there isn't and that's intentional. The standard library implementation should have a minimum of inter dependencies for the implemented components.

You should always specify the #include statements for the std components you use explicitly.


And don't be tricked by the infamous #include <bits/stdc++.h>.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
4

Include lots of unused headers will drastically increase build time! Add only needed files

Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85
2

You could simply create a pre-compiled-header file for yourself of often included to speed up builds. It was popular last millennium.

See this Wikipedia article on the subject.

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
1

You can try:

#ifndef ALL_H_FILES
#define ALL_H_FILES

#include <iostream>
#include <vector>
#include <list>
//include other libraries

#endif

On your main file, you can: #include "ALL_H_FILES.h"

stelity
  • 33
  • 8
1

Rejoice! C++23 shall save you effort of including everything. You can just do:

import std; //imports everything in std library
import std.compat; //brings c library to global namespace

Although you may need to wait for a year or three for compilers to support it.

0xB00B
  • 1,598
  • 8
  • 26
0

Instead of including different libraries for different functions you can simply just add

#include <bits/stdc++.h>

at the top of your cpp program.