为了使用字符串,我需要包含字符串标头,以便其实现可用。但如果是这样,为什么我还需要添加这一行using std::string
?
为什么它还不知道字符串数据类型?
#include <string>
using std::string;
int main() {
string s1;
}
Because string
是在命名空间中定义的std
.
你可以写std::string
无处不在<string>
已包含,但您可以添加using std::string
并且不要在范围内使用名称空间(所以std::string
可能被称为string
)。例如,您可以将其放置在函数内部,然后它仅适用于该函数:
#include <string>
void foo() {
using std::string;
string a; //OK
}
void bar() {
std::string b; //OK
string c; //ERROR: identifier "string" is undefined
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)