#include <iostream>
int main() {
std::string a="hello";
std::cout<< a;
}
The above code prints out "hello". Then, why do people insist upon including the <string>
header file?
#include <iostream>
int main() {
std::string a="hello";
std::cout<< a;
}
The above code prints out "hello". Then, why do people insist upon including the <string>
header file?
There is no guarantee your code will work without <string>
. That is the header std::string
is defined in, so you must include it if you want your code to be portable.
Your code may work or appear to work because <iostream>
directly or indirectly includes <string>
.
I think your compiler is just being nice to you. If you are using strings, you should #include <string>
, whether or not your particular compiler lets you get away without it. Moreover, apparently <iostream>
includes <string>
, directly or indirectly, but don't rely on it.