可以在没有#include< string>的情况下使用std :: string吗? [英] Can std::string be used without #include <string>?
问题描述
这是我的代码:
#include <iostream>
int main(int argc, char const *argv[])
{
std::string s = "hello";
std::cout << s.size() << std::endl;
return 0;
}
令我惊讶的是,我什至不用添加 #include< string>
.我可以使用 clang ++
编译并运行它.
To my surprise, I can compile and run it with clang++
, though I even don't add #include <string>
.
因此,是否需要添加 #include< string>
才能使用 std :: string
?
So, is it necessary to add #include <string>
in order to use std::string
?
推荐答案
您实现的 iostream
标头包含 string
.这不是您可以依靠或应该依靠的东西.如果要使用 std :: string
,则应始终 #include< string>
,否则您的程序可能无法在不同的实现上运行,甚至无法在更高版本的您当前的那个.
Your implementation's iostream
header includes string
. This is not something which you can or should rely on. If you want to use std::string
, you should always #include <string>
, otherwise your program might not run on different implementations, or even in later versions of your current one.
这篇关于可以在没有#include< string>的情况下使用std :: string吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!