Using std Namespace(使用 std 命名空间)
问题描述
对于 std 命名空间使用using"似乎有不同的看法.
There seem to be different views on using 'using' with respect to the std namespace.
有些人说使用using namespace std
",其他人说不要,而是为要与std::
"一起使用的 std 函数添加前缀,而其他人则说使用这样的东西:
Some say use ' using namespace std
', other say don't but rather prefix std functions that are to be used with ' std::
' whilst others say use something like this:
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::vector;
用于所有要使用的 std 函数.
for all the std functions that are to be used.
各自的优缺点是什么?
推荐答案
大多数 C++ 用户都非常喜欢阅读 std::string
、std::vector
等.事实上,看到一个原始的 vector
让我想知道这是 std::vector
还是不同的用户定义的 vector
.
Most C++ users are quite happy reading std::string
, std::vector
, etc. In fact, seeing a raw vector
makes me wonder if this is the std::vector
or a different user-defined vector
.
我总是反对使用 using namespace std;
.它将各种名称导入全局命名空间,并可能导致各种不明显的歧义.
I am always against using using namespace std;
. It imports all sorts of names into the global namespace and can cause all sorts of non-obvious ambiguities.
以下是 std
命名空间中的一些常见标识符:count、sort、find、equal、reverse.有一个名为 count
的局部变量意味着 using namespace std
不会让你使用 count
而不是 std::count代码>.
Here are some common identifiers that are in the std
namespace: count, sort, find, equal, reverse. Having a local variable called count
means that using namespace std
won't enable you to use count
instead of std::count
.
不需要的名称冲突的经典示例如下所示.想象一下,您是初学者并且不了解 std::count
.想象一下,你要么在 中使用了其他东西,要么它被一个看似无关的标头拉进来了.
The classic example of an unwanted name conflict is something like the following. Imagine that you are a beginner and don't know about std::count
. Imagine that you are either using something else in <algorithm>
or it's been pulled in by a seemingly unrelated header.
#include <algorithm>
using namespace std;
int count = 0;
int increment()
{
return ++count; // error, identifier count is ambiguous
}
该错误通常很长且不友好,因为 std::count
是具有一些长嵌套类型的模板.
The error is typically long and unfriendly because std::count
is a template with some long nested types.
不过这没问题,因为 std::count
进入全局命名空间,而函数 count 将它隐藏起来.
This is OK though, because std::count
goes into the global namespace and the function count hides it.
#include <algorithm>
using namespace std;
int increment()
{
static int count = 0;
return ++count;
}
也许有点令人惊讶,这还可以.导入声明性作用域的标识符出现在包含定义它们的位置和导入它们的位置的公共命名空间中.换句话说,std::count
在全局命名空间中作为 count
可见,但只在 increment
内部可见.
Perhaps slightly surprisingly, this is OK. Identifiers imported into a declarative scope appear in the common namespace that encloses both where they are defined and where they are imported into. In other words, std::count
is visible as count
in the global namespace, but only inside increment
.
#include <algorithm>
int increment()
{
using namespace std;
static int count = 0;
return ++count;
}
出于类似的原因,count
在这里是模棱两可的.using namespace std
不会导致 std::count
,如预期的那样隐藏外部 count
.using namespace
规则意味着 std::count
看起来(在 increment
函数中)好像它是在全局范围内声明的,即在与 int count = 0;
的范围相同,因此导致歧义.
And for similar reasons, count
is ambiguous here. using namespace std
doesn't cause std::count
, hide the outer count
as it might be expected. The using namespace
rule means that std::count
looks (in the increment
function) as though it was declared at the global scope, i.e. at the same scope as int count = 0;
and hence causing the ambiguity.
#include <algorithm>
int count = 0;
int increment()
{
using namespace std;
return ++count; // error ambiguous
}
这篇关于使用 std 命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 std 命名空间
基础教程推荐
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01