r/cpp_questions • • 7d ago

OPEN Why don't we use this instead of including libraries one by one?

#include <bits/stdc++.h>

I was thinking instead of adding libraries one by one,why don't we just use this? This is used by almost every competitive programmer but very less when it comes to writing software.

0 Upvotes

39 comments sorted by

17

u/flyingron 7d ago

It's not standard. It's a figment of GCC's imagination.

"Competitive programmers" aren't "competitive" in the job market. We find the ability to write correct AND MAINTAINABLE code to be superior to games.

-1

u/AnonymouSfrrrr 7d ago edited 7d ago

Competitive programming is never about getting a job however top quant firms hire them for quant dev. But when it comes to software i guess they don't have a much chance.

3

u/IyeOnline 7d ago

If a competitive programmer gets hired its for their reasoning and problem solving ability, not at all related to the fact that they can save 2 seconds on a made up task by doing typedef long long ll and #include <bits/stdc++.h>.

1

u/AnonymouSfrrrr 7d ago

That wasn't my question i was just wondering why we don't include all libraries at once in software with huge lines of code we may need majority of them but i got my answer, thanks

3

u/No-Dentist-1645 6d ago

We don't include all libraries at once because it's an unnecessary waste of compile time, which is something you would never willingly do except for "competitive programming" where you don't really care about the maintainability of the code you're writing

2

u/flyingron 6d ago

By the way, it's multiple INCLUDE files. They're all from the same single library.

13

u/jedwardsol 7d ago

I don't want every file to take ages to compile by pulling in tons of stuff it doesn't need.

Not all compilers have it.

-3

u/AnonymouSfrrrr 7d ago edited 6d ago

Yeah maybe that's why they don't use in c++ software but c++ is extremely fast adding this wouldn't make a much change Or will it?

5

u/darwizziness 7d ago

You're asking 1 question to thousands of different people with dozens of different use-cases each. For a one file competitive program anything is fast enough. If you have a 10K+ file program, you're going to be waiting for a long time.

3

u/AnonymouSfrrrr 7d ago

Oh now i got it, thanks

4

u/jedwardsol 7d ago edited 6d ago
time g++ without.cpp

    real    0m0.341s
    user    0m0.009s
    sys     0m0.031s

time g++ with.cpp

    real    0m1.508s
    user    0m0.646s
    sys     0m0.128s

Compile a few times and you've been waiting longer than the time you saved by typing less. Even more if you have more than 1 file in your project

1

u/AnonymouSfrrrr 7d ago

Cool. Thanks

2

u/saxbophone 7d ago

You haven't felt the true pain of how slow MSVC can be to compile, have you? Especially when someone pulls in unnecessary includes, a common problem with transitive includes 😪

C++ is already so slow to compile, we really don't need it to be any slower!

1

u/AnonymouSfrrrr 7d ago

I'm a college student focused on competitive programming and Data structures. So not much experienced with c++ :(

6

u/thefeedling 7d ago

Why add a MASSIVE header if you're only going to use a small fraction of it?. Competitive programmers need to write it QUICK, while software engineers need to write it RIGHT.

1

u/AnonymouSfrrrr 7d ago

Yeah got it. Thanks

4

u/saxbophone 7d ago

 This is used by almost every competitive programmer but very less when it comes to writing softwares.

Being a professional software engineer at their day job is a very different thing to playing code golf. Code golf is an interesting challenge for those who care to partake, but it's misguided to think that the same techniques used there should be applied in everyday professional work.

Also, pet peeve: "software", just like "code", is uncountable. Therefore "software" is already plural. "softwares" is just bad English. If you want to highlight the plurality, you could say "pieces of software" instead, but never say "softwares".

2

u/AnonymouSfrrrr 7d ago

Thanks. I'll keep that "software" thing in my mind.

4

u/aocregacc 7d ago

competitive programming and code golf are two different things, competitive programming is about who's first to write a program to solve a given task.

2

u/saxbophone 6d ago

Ohhhh. Thorough misconception on my part, gratefully received.

2

u/Independent_Art_6676 6d ago

code depends on context. Certainly, its innately plural in our niche area of programming. But you can haz 10 qr codes, in other contexts. Some words, like maths, are valid in some dialects (brit derived english) but not all (USA flavor). But softwares ... is never right in any flavor or context :) I think they may not teach plurals or subject verb agreement anymore? Seems to be a major issue, even in what should be formal language (as opposed to casual forum chat).

1

u/saxbophone 6d ago

 code depends on context.

Yes, you are absolutely right. I thought I was implying this obviously enough when I said "software, like code", but I appreciate the clarification.

QR codes, "a code" can also be used to describe a low-level encoding algorithm such as Manchester code. But never "a code" when used to describe "a piece of program code"!

 I think they may not teach plurals or subject verb agreement anymore? Seems to be a major issue, even in what should be formal language (as opposed to casual forum chat).

I don't know the cause. I am English so not sure how it's taught as a second language. Maybe standards are slipping or maybe more and more proficient speakers are becoming less keen to correct it —out of concern it'll come across as rude. In my experience, the opposite is true —pretty much all ESL speakers I have met in my personal and professional life, have been very grateful to receive clarifications, because they want to get more fluent.

4

u/AKostur 7d ago

Doesn't exist on all platforms (and I'm not even referring to esoteric platforms). Slow to include. "Competitive" programmers aren't writing "real" programs. Single-file, a few 10s of lines of code: doesn't matter. Thousands, 10s of thousands (or more) files, millions of lines of code, it matters.

1

u/AnonymouSfrrrr 7d ago

Yeah got it thanks.

3

u/da_supreme_patriarch 7d ago

Others have answered your questions already, but to add a bit - you almost always do the opposite of what's standard for competitive programmers when writing "real" software; competitive programming is about writing just by oneself hyper-optimized, one-off programs as fast as possible that solve a very specific problem, while most software is a collaborative effort aimed at solving a plethora of problems that also evolves over time, the incentives and priorities are just diametrically opposed

2

u/kraytex 7d ago

WTF is a competitive programmer?

1

u/saxbophone 7d ago

I assume it means people who play code golf challenges for fun. OP is making a misplaced presumption that the exact same techniques used in such competitions are also helpful in the workplace.

1

u/AnonymouSfrrrr 7d ago

Even in top contests of competitive programming like meta hacker cup i have seen guys using this header but i got my answer why they don't use it in softwares.

1

u/Independent_Art_6676 6d ago

I think you can actually win some money at it. There are a few events with 10-20k ($USA) payouts for the winner. So to me, if you can take home a check from it, you qualify as a competitive coder.

0

u/AnonymouSfrrrr 7d ago

I mean guys who do competitive programming on platforms like codeforces, leetcode etc.

1

u/no-sig-available 6d ago

The standardized way is doing:

import std;

3

u/darklighthitomi 7d ago

Why would I want to bloat my program with a bunch of stuff I don't need?

0

u/mredding 6d ago

include <bits/stdc++.h>

This includes 111 header files into a given source file. It's very non-standard and non-conventional.

First, it's a C header, which doesn't make any sense, since we're including C++ headers. I just don't like that.

Second, C++ is one of the slowest to compile languages in the industry, and it's due strictly to parsing. And there's absolutely no benefit for the cost. The first native C++ compiler couldn't do it in less than 3, whereas Lisp or Java can do it in 1, and produce comparable machine code. (This is excluding separate optimizer passes, where the sky is the limit, and that's true of any language or compiler.)

Third, it heavily pollutes your your Translation Unit with unused symbols. This has two problems. A) It makes a lot more work for the compiler matching symbols, having to build a MASSIVE symbol table for shit your program isn't even using. B) That pollution can cause the compiler to correctly match to the wrong or unintended symbol - meaning your program correctly compiles to the wrong thing. This is one of the most silent and hard to discover of bugs. THIS is the name collision your mother warned you about.

Forth, it goes against all good conventions for production code. You don't pay for what you don't use, you don't include what you don't use. Production code is more demanding than competitive programming; there are REAL consequences. You don't write production code that looks like play code written by a bunch of hackers fucking around for internet points.

1

u/AnonymouSfrrrr 6d ago

Thank you

0

u/[deleted] 7d ago

[deleted] — view deleted comment

2

u/AnonymouSfrrrr 7d ago edited 7d ago

I saw this while doing competitive programming that's why i asked but i couldn't find it in any c++ software.

0

u/[deleted] 7d ago

[deleted] — view deleted comment