首页 文章

总是“使用命名空间std”? [重复]

提问于
浏览
-2

这个问题在这里已有答案:

我能理解为什么我们应该在代码中使用这一行 . 这样,您就不必编写std :: cout或std :: cin等 .

对于std :: string,如果我包含在c代码中,编译器是否会感到困惑?对于下面的变量str,它被认为是cstring类型的字符串还是std :: string?

include <cstring>
include <string>

using namespace std;

string str;

换句话说,如果我在代码的开头有“using namespace std”,将所有“std :: string”简单地写为“string”是否安全?另外如果我有“using namespace std”,我不需要“使用std :: string”,对吗?

1 回答

  • 5

    没有"cstring type of string" . <cstring> 标头不包含字符串类,只包含函数(以及 size_t 类型和 NULL 宏) . 在您的示例中, string 将被视为 std::string .

    至于 using namespace . 我通常只在非常小的范围内使用它,例如函数体内部 . 从不在头文件中!

相关问题