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

KOB picture KOB · May 12, 2013 · Viewed 16.1k times · Source

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?

Answer

Luchian Grigore picture Luchian Grigore · May 12, 2013

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.