Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to use #include <string> beside <iostream>?

Tags:

I started learning C++ and I read a book which writes that I must use the <string> header file because the string type is not built directly into the compiler. If I use the <iostream> I can use the string type.

Do I have to include the <string> header when I want to use the string type if I included the <iostream> header? Why? Is there some difference?

like image 588
KOB Avatar asked May 12 '13 09:05

KOB


People also ask

Is temperature screening still required?

Temperature screening and symptom checks no longer required From 19 August 2021, temperature screening and symptom checks are no longer required at workplaces.

Do we need MC for Covid?

There is no need for these employees to visit a General Practitioner (GP), polyclinic or Emergency Department in the hospital, or any medical professional for that matter, just to undergo a confirmatory Polymerase Chain Reaction ( PCR ) test2 or to obtain a medical certificate (MC) or recovery memo.

How long does Covid last in your body?

Most people with COVID-19 get better within a few days to a few weeks after infection, so at least four weeks after infection is the start of when post-COVID conditions could first be identified. Anyone who was infected can experience post-COVID conditions.

What should I do if I test positive?

Stay at home advice If you have a positive coronavirus test result, try to stay at home and avoid contact with other people for 5 days after the day you took your test, or from the day your symptoms started (whichever was earlier). You should count the day after you took the test as day 1.


2 Answers

Yes, you have to include what you use. It's not mandated that standard headers include one another (with a few exceptions IIRC). It might work now, but might fail on a different compiler.

In your case, apparently <iostream> includes <string>, directly or indirectly, but don't rely on it.

like image 158
Luchian Grigore Avatar answered Oct 07 '22 15:10

Luchian Grigore


Do I have to include the <string> header when I want to use the string type if I included the <iostream> header?

Yes, you have to. You cannot rely on relevant headers (e.g. <string>) being #included indirectly through other headers (e.g. <iostream>), although this might be the case on some implementations.

And even when this may seem to work, it could lead to troubles if not all of the relevant overloads of some operators are imported, or if a class is forward-declared in a header you #include, but information on that class being derived from some other class is only contained in a header that does not get #included.

See, for instance, this Q&A on StackOverflow for an example of such situations.

like image 20
Andy Prowl Avatar answered Oct 07 '22 15:10

Andy Prowl