what is difference between iostream and bits/stdc++.h?? when do we need to use them? - header-files

I am confused with the differences between iostream and bits/stdc++.h ? in competitive programming is it okay to use bits/stdc++.h or are there any consequences

You can't really compare the two. iostream is a header file that allows you to use input (cin) and output (cout). A header file is basically just a file with a collection of functions you can use to make coding easier. This is similar to the built in library in Python (Ex: import random). bits/stdc++.h is basically a way to import every single C++ header file. Many competitive programmers use this because they don't have to import every popular header file. However, a disadvantage is that it increases compilation time because it has to search through every possible functions. Most people do not find this to be too much of an issue, however, so you can probably go ahead and use it. However, it is considered bad practice to do this in actual software engineering. Hope this helps and have a good day :)

Related

C++ #include statement

I am a Java developer and C++ beginner. In Java, I can import objects easily using (Ctrl + Shift + O). In C++ however, I have to manually type #include each time, wasting my time. In addition, I often don't know where the required object is.
Is there an easy way to import or type "#include" automatically? If not, is there a plug-in or add-on to do that? I am using Eclipse IDE for Blackberry 10, along with C++ Cascades.
This is just how C++ works. The #include functionality is primitive compared to a Java import: each #include is simply replaced by the text of the included file (and so on, recursively) as if it had been copied and pasted in there.
This sometimes has advantages, and it's certainly simple, but it does mean that there's no reliable way to know ahead of time what is defined by a particular included file. So, if you need the vector type, for example, that is in vector; but if you need the va_list type, that is in stdarg.h. Generally, things are reasonably consistent, but not perfectly so, and there's nothing to enforce it anyway. This is probably why most IDEs don't provide much help for it. You just need to know what the rules are (if there are any) for the libraries you're using.
IDE support for C++ is generally not as good as it is for Java or C#. This is one example (there are plenty of other ones). If you are expecting a Java or C#-level of assistance, you are likely to end up disappointed. On the plus side, while sorting out the #include list is annoying, there are lots of other difficulties encountered when working with C++, so it rarely ends up the main problem.
see this bug report.
It seems that people have been discussing this for about 10 years but it's not implemented yet.
Personally I believe as a C++ programmer you should be trying to eliminate excessive use of include's in your files and use forward declarations instead therefore it's not a feature many programmers are looking for. If you prefer not to have that much control over the program, you can always code in java or c#.

What objective reasons for coding without headers in C or C++ are there? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Coding C++ without headers, best practices?
Inspired by this question - the OP just finds the concept of headers confusing and depressing there, it's his subjective experience.
Now are there any objective reasons and scenarios for avoiding headers when coding in C or C++?
There are no objective reasons, only irrational ones borne of subjective bias induced by experiences with other languages for which headers don't exist as a concept.
The compilation unit is a fundamental concept in C and C++ compilers. A compilation unit must have access to all declarations at the source code level in order to access functionality provided by other compilation units, and the only logically coherent way to ensure that publishers and consumers of functionality see the declarations is for them to share the source code for those declarations. Headers are the natural way to achieve this.
Being a bit more technical about it, headers are not a C (or C++) concept, per se. They are preprocessor concept. When processing a source file, whenever the preprocessor encounters a #include directive, it replaces the directive with the entire contents of the header being referred to, recursively expanding any #include directives in the header itself. By the time the compiler proper kicks in, it sees nothing but a single "file" containing all the declarations it needs to do its job.
The key point is that, for better or worse, headers are the standard way to organise complex C and C++ code bases. You can play fun and games with #ifdefs to put both "header" and implementation in one source file, but that works against every tool-chain in the universe, and means that preprocessors have far more work to do, thus dramatically slowing down your builds.
None at all, it's just a bad idea.
The only objective reason would be if your C compiler could only include an extremely limited number of files over the course of a compilation, or the system you are working on having an extremely limited number of inodes.
#includeing a header file tells the compiler, quite literally, "copy-paste some text here." Now perhaps your question is really asking, "are there any misuses for header files?" (and there certainly are), but as written, it makes as little sense as, "are there any objective reasons to avoid source files?"
I found header files great before I worked with java. Now I find them annoying, because it is code duplication, you have to keep 2 files in sync.
Of course, if you have a tool which builds the header dynamically, it's not that bad. Are there such tools?
No, header files are an essential tool and cannot be removed in the language as it is. That doesn't mean that they don't completely suck and shouldn't be removed ASAP.
I have experienced one good reason for avoiding headers in C: distributing an open source application as a single .c file.
This is the distribution mode of dcraw, an ANSI-C cross-platform software for converting digital camera RAW files.

Include everything, Separate with "using"

I'm developing a C++ library. It got me thinking of the ways Java and C# handle including different components of the libraries. For example, Java uses "import" to allow use of classes from other packages, while C# simply uses "using" to import entire modules.
My questions is, would it be a good idea to #include everything in the library in one massive include and then just use the using directive to import specific classes and modules? Or would this just be down right crazy?
EDIT:
Good responses so far, here are a few mitigating factors which I feel add to this idea:
1) Internal #includes are kept as normal (short and to the point)
2) The file which includes everything is optionally supplied with the library to those who wish to use it3) You could optionally make the big include file part of the pre-compiled header
You're confusing the purpose of #include statements in C++. They do not behave like import statements in Java or using statements in C#. #include does what it says; namely, loads and parses the entire indicated file as part of the current translation unit. The reason for the separate includes is to not have to spend compilation time parsing the entire standard library in every file. In contrast, the statements you're trying to make #include behave like are merely for programmer organization purposes.
#include is for management of the compilation process; not for separating uses. (In fact, you cannot use seperate headers to enforce seperate uses because to do so would violate the one definition rule)
tl;dr -> No, you shouldn't do that. #include as little as possible. When your project becomes large, you'll thank yourself when you're not waiting many hours to compile your project.
I would personally recommend only including the headers when you need them to explicitly show which functionalities your file requires. At the same time, doing so will prevent you from gaining access to functionalities you might no necessarily want, e.g functions unrelated to the goal of the file. Sure, this is no big deal, but I think that it's easier to maintain and change code when you don't have access to unnecessary functions/classes; it just makes it more straightforward.
I might be downvoted for this, but I think you bring up an interesting idea. It would probably slow down compilation a bit, but I think the concept is neat.
As long as you used using sparingly — only for the namespaces you need — other developers would be able to get an idea of what classes were used in a file by glancing at the top. It wouldn't be as granular as seeing a list of #included files, but is seeing a list of included header files really very useful? I don't think so.
Just make sure that all of the header files all use inclusion guards, of course. :)
As said by #Billy ONeal, the main thing is that #include is a preprocessor directive that causes a "^C, ^V" (copy-paste) of code that leads to a compile time increase.
The best considered policy in C++ is to forward declare all possible classes in ".h" files and just include them in the ".cpp" file. It isolates dependencies, as a C/C++ project will be cascadingly rebuilt if a dependent include file is changed.
Of course M$ compilers and its precompiled headers tend to do the opposite, enclosing to what you suggest. But anyone that tried to port code across those compilers is well aware of how smelly it can go.
Some libraries like Qt make extensive use of forward declarations. Take a look on it to see if you like its taste.
I think it will be confusing. When you write C++ you should avoid making it look like Java or C# (or C :-). I for one would really wonder why you did that.
Supplying an include-all file isn't really that helpful either, as a user could easily create one herself, with the parts of the library actually used. Could then be added to a precompiled header, if one is used.

Consistenty header file names between C++ libraries

In my project I use two libraries, v8 and boost. Boost uses the .hpp extension for its headers, while v8 uses the .h extension for its headers.
In the end of day, my source code starts like that:
#include "v8.h"
#include "boost/filesystem.hpp"
...
In other question I asked about this subject, the general answer was that it is okay, but I just should be consistent between names.
This code compiles well, but, coding styles/standards - is it okay? Is there any solution for this problem (like changing all .hpp to .h automatically somehow?)
Thanks. And sorry for those stupid questions.
Don't worry about the inconsistency, it doesn't matter. Too much time is often spent obsessing about such details, and everyone is guilty of it.
Just be consistent with your own coding standards.
You'll eventually use some 3rd party library or several that use different conventions than you. There's nothing you can do about it, and often 2 of those libraries you use will be conflicting with your standards and with each other. That's not only for include extensions, but also for naming convetions like function_that_does_something vs FunctionThatDoesSomthing .It's fine.
I would definitely strongly advice against trying to change someone else's library to fit into your coding standard. I.e. for example renaming boost .hpp to .h. This is a bad idea and when you want to upgrade to newer versions of the library it will be a nightmare.
Spend your time solving the problem you're solving in a more elegant way rather than worrying about details like this.
It's fine. Coding standards don't really come into it since you have to go with what you're given. If the v8 people only provide .h and the boost people only provide .hpp then, short of copying one set of files to the other choice or providing your own wrapper header files, you have few options.
Both of those option have their downsides for what is really dubious benefits, so I wouldn't concern yourself with the fact that you have to include two different file extensions.

What's the rationale behind headers?

I don't quite understand the point of having a header; it seems to violate the DRY principle! All the information in a header is (can be) contained in the implementation.
It simplifies the compilation process. When you want to compile units independently, you need something to describe the parts that will be linked to without having to import the entirety of all the other files.
It also allows for code hiding. One can distribute a header to allow others to use the functionality without having to distribute the implementation.
Finally, it can encourage the separation of interface from implementation.
They are not the only way to solve these problems, but 30 years ago they were a good one. We probably wouldn't use header files for a language today, but they weren't invented in 2009.
The architects of many modern languages such as Java, Eiffel and C# clearly agree with you -- those languages extract the metadata about a module from the implementation. However, per se, the concept of headers doesn't preclude that -- it would obviously be a simple task for a compiler to extract a .h file while compiling a .c, for example, just like the compilers for those other languages do implicitly. The fact that typical current C compilers do not do it is not a language design issue -- it's an implementation issue; apparently there's no demand by users for such a feature, so no compiler vendor bothers implementing it.
As a language design choice, having separate .h files (in a human-readable and editable text format) gives you the best of both worlds: you can start separately compiling client code based on a module implementation that doesn't yet exist, if you wish, by writing the .h file by hand; or you (assuming by absurd a compiler implementation that supplies it;-) can get the .h file automatically from the implementation as a side effect of compiling it.
If C, C++, &c, keep thriving (apparently they're still doing fine today;-), and demand like yours for not manually writing headers grows, eventually compiler writers will have to supply the "header generation" option, and the "best of both worlds" won't stay theoretical!-)
It helps to think a bit about the capabilities of the computers that were available when, say c, was written. Main memory was measured in kilowords, and not necessarily very many of them. Disks were bigger, but not much. Serrious storage meant reel-to-reel tapes, mounted by hand, by grumpy operators, who really wanted you to go away so they could play hunt the wumpus. A 1 MIPS machine was screaming fast. And with all these limitation you had to share it. Possibly with a score of other users.
Anything that reduced the space or time complexity of compilation was a big win. And headers do both.
Don't forget the documentation a header provides. There is usually anything in it you need to know for using the module. I for my part don't want to scan through a looong sourcecode to learn what there is that I need to use and how to call it... You would extract this information anyway, which effectively results in -- a header file. No longer an issue with modern IDEs, of course, but working with some old C code I really love to have hand-crafted header files that include comments about the usage and about pre- and postconditions.
Keeping source, header and additional documentation in sync still is another can of worms...
The whole idea of inspecting the binary output files of language processors would have been hard to comprehend when C invented .h files. There was a system called JOVIAL that did something like it, but it was exotic and confined more-or-less exclusively to military projects. (I've never seen a JOVIAL program, I've only heard about it.)
So when C came out the usual design pattern for modularity was "no checks whatsoever". There might be a restriction that .text symbols could only link to .text and .data to .data, but that was it. That is, the compilers of the day typically processed one source file at a time and then linkers put them together without the slightest level of error checking other than, if you were lucky, "I'm a function symbol" vs "I'm a data symbol".
So the idea of actually having the compiler understand the thing you were calling was somewhat new.
Even today, if you make a totally bogus header, no one catches you in most AOT compilers. Clever things like CLR languages and Java actually do encode things in the class files.
So yes, in the long run, we probably won't have header files.
No you dont have headers in Java -- but you do have interfaces and I every serious Java guru recommends you define anything used by other projects/systems as an interface and an implementation.
Lets see a java interface definition contains call signatures, type definitions and contants.
MOST C header files contain call signatures, type definitions and constants.
So for all pratical purposes C/C++ header files are just interface definitions and should thus be considered a Good Thing. Now I know its possible to define a myriad other things in header files as well (MARCROs, constants etc. etc. ) but that just part of the whole wonderful world of C:-
int function target () {
// Default for shoot
return FOOT;
}
For Detail Read this
A header file commonly contains forward declarations of classes, subroutines, variables, and other identifiers. Programmers who wish to declare standardized identifiers in more than one source file can place such identifiers in a single header file, which other code can then include whenever the header contents are required.
The C standard library and C++ standard library traditionally declare their standard functions in header files.
And what if you want to give somebody else the declarations to use your library without giving them the implementation?
As another answer points out - the original reason for headers was to make the parse/compile easier on platforms with very simple and limited tools. It was a great step forward to have a machine with 2 floppies so you could have the compiler on one and your code on the other - made things a lot easier.
When you divide code in header and source files you divide declaration and definition. When you look in header files you can see what you have and if you wand to see implementation details you go to source file.