I looked up header file examples, but could only find simple ones with nothing to include.
So my question is where do I #include
stuff like string
and vector
? In the .h or the .cpp file? Or maybe in both?
English, U.S. Strictly speaking, "where" is an adverb. Adding prepositions to it ("to", "at") would be treating it like a noun or pronoun (and some people do this in speech). In standard English, the preposition is superfluous and "wrong".
Definition of whereto (Entry 1 of 2) : to what place, purpose, or end whereto tends all this— William Shakespeare. whereto. conjunction.
Where-you-at definition. Filters. (proscribed, nonstandard, slang, set phrase) "Where are you?" Used as an inquiry of location, especially figuratively.
To is a preposition with several meanings, including “toward” and “until.” Too is an adverb that can mean “excessively” or “also.” Just to be clear: two is pronounced the same as to and too, but it can't be used instead of either of them because it's a number.
Anywhere you need.
If you need something declared in a header file, include them in the .h file. Otherwise just in the .cpp file.
Note that including a .h file is just a textual replacement and the contents of the included .h file will be entirely inserted at the beginning of the file where they've been included. at the precise point of the #include
line.
It is good practice to include a project-belonging headers before the standard library ones and never include a header if you don't need it.
Last thing you should keep in mind is that when working with large projects including many headers in a .h file shared by many translation units can increase compilation times if the header gets modified. It's usually preferred to just include what you strictly require in the appropriate files (either .cpp or .h). Precompiled headers might also help but it's off-topic to your question.
Finally: don't rely on "this header has been included somewhere else and I'm already including it through a second header" because it could render dependencies-tracking hard and favor circular dependencies when the project grows.
where do I #include stuff like string and vector? In the .h or the .cpp file?
There are multiple issues to consider in this, especially when projects get bigger (i.e. the bigger your project is, the more this affects you).
Personally, I follow these rules:
if code needs a header to compile, then you need to include it (if header declares things with std::string in the API, you will have to include string
and the same goes for the C++ file)
do not include headers that are not needed (i.e. not "both" - if you include a header in your .h file, then include your .h file, you should be fine).
organize headers prioritizing for your project's files. this means if you have in a C++ file local project headers, and std and boost headers, you should (probably) include local project headers first, then boost, then std.
This is because the std headers will be the most tested/stable ones, and the most used API (this is a blind supposition on my part). If you were to include std headers first, and then project headers (for example), since the replacement is textual, you could get away with not adding the include in the project header. This would basically mask an error, because you would end up having to include the std headers before the local project header in all other cpp files from now on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With