我能理解为什么我们应该在代码中使用这一行.这样,您就不必编写std :: cout或std :: cin等.
对于std :: string,如果我包含在c ++代码中,编译器是否会感到困惑?对于下面的变量str,它被认为是cstring类型的字符串还是std :: string?
include <cstring>
include <string>
using namespace std;
string str;
Run Code Online (Sandbox Code Playgroud)
换句话说,如果我在代码的开头有"using namespace std",那么将所有"std :: string"简单地写为"string"是否安全?另外如果我有"using namespace std",我不需要"使用std :: string",对吗?
没有"cstring类型的字符串".所述<cstring>报头不包含字符串类,只有功能(以及size_t类型和NULL宏).在你的例子中,string只会被考虑std::string.
至于using namespace.我通常只在非常小的范围内使用它,例如函数体内部.从不在头文件中!