Https://www

     
I have sầu read from a codeforces blog that if we add #include in a C++ program then there is no need to include any other header files. How does #include work & is it ok khổng lồ use it instead of including individual header files?


*

*

It is basically a header file that also includes every standard library & STL include file. The only purpose I can see for it would be for testing and education.

Bạn đang xem: Https://www

Se e.g. GCC 4.8.0 /bits/stdc++.h source.

Using it would include a lot of unnecessary stuff & increases compilation time.

Edit: As Neil says, it"s an implementation for precompiled headers. If you set it up for precompilation correctly it could, in fact, speed up compilation time depending on your project. (https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html)

I would, however, suggest that you take time to learn about each of the sl/stl headers & include them separately instead, and not use "super headers" except for precompilation purposes.


Share
Follow
edited May 25 "trăng tròn at 18:52
*

bscharan
2511 silver badge88 bronze badges
answered Aug 14 "14 at 14:54
*

ZelixZelix
1,70411 gold badge1313 silver badges1111 bronze badges
6
| Show 1 more comment
48
#include is an implementation tệp tin for a precompiled header.

From, software engineering perspective, it is a good idea to lớn minimize the include. If you use it actually includes a lot of files, which your program may not need, thus increase both compile-time và program kích cỡ unnecessarily. as pointed out by
Swordfish in the comments that the output program kích thước remains unaffected. But still, it"s good practice lớn include only the libraries you actually need, unless it"s some competitive competition >

But in contests, using this tệp tin is a good idea, when you want to lớn reduce the time wasted in doing chores; especially when your rank is time-sensitive.

It works in most online judges, programming condemo environments, including ACM-ICPC (Sub-Regionals, Regionals, và World Finals) & many online judges.

The disadvantages of it are that it:

increases the compilation time.uses an internal non-standard header tệp tin of the GNU C++ library, & so will not compile in MSVC, XCode, and many other compilers
Share
Follow
edited Aug 28 at 2:30
*

Nick Dong
3,18166 gold badges4343 silver badges7171 bronze badges
answered Nov 17 "15 at 18:12
abe312abe312
4,2753232 silver badges2121 bronze badges
7
| Show 2 more comments
30
That header tệp tin is not part of the C++ standard, is therefore non-portable, and should be avoided.

Xem thêm: Tổng Quan Ứng Dụng Của Blockchain Và Các Ứng Dụng Trong Thực Tiễn

Moreover, even if there were some catch-all header in the standard, you would want lớn avoid it in lieu of specific headers, since the compiler has khổng lồ actually read in & parse every included header (including recursively included headers) every single time that translation unit is compiled.


Share
Follow
answered Aug 14 "14 at 14:48
Robert Allan Hennigan LeahyRobert Allan Hennigan Leahy
6,1182525 silver badges4545 bronze badges
5
Add a phản hồi |
3
Unfortunately that approach is not portable C++ (so far).

All standard names are in namespace std and moreover you cannot know which names are NOT defined by including và header (in other words it"s perfectly legal for an implementation to lớn declare the name std::string directly or indirectly when using #include ).

Despite this however you are required by the language khổng lồ know and tell the compiler which standard header includes which part of the standard library. This is a source of portability bugs because if you forget for example #include but use std::bản đồ it"s possible that the program compiles anyway silently và without warnings on a specific version of a specific compiler, & you may get errors only later when porting to lớn another compiler or version.

In my opinion there are no valid technical excuses that explain why this is necessary for the general user: the compiler binary could have sầu all standard namespace built in và this could actually increase the performance even more than precompiled headers (e.g. using perfect hashing for lookups, removing standard headers parsing or loading/demarshalling & so on).

The use of standard headers simplifies the life of who builds compilers or standard libraries and that"s all. It"s not something lớn help users.

However this is the way the language is defined & you need lớn know which header defines which names so plan for some extra neurons to lớn be burnt in pointless configurations to rethành viên that (or try khổng lồ find & IDE that automatically adds the standard headers you use và removes the ones you don"t... a reasonable alternative).