C++:关于使用命名空间 std 和 cout 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15346944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: Questions about using namespace std and cout
提问by Justin Liang
Why do I need to type in using namespace std;
in order to be able to use cout
and endl
? Also what are these called; is cout
a function?
为什么我需要输入using namespace std;
才能使用cout
和endl
?还有这些叫什么?是cout
函数吗?
Is there cout
in C? I heard it was implemented in C++ because it is better in many ways.
cout
C里面有吗?我听说它是用 C++ 实现的,因为它在很多方面都更好。
回答by Andy Prowl
cout
is a global object defined in the std
namespace, and endl
is a (stream manipulator) function also defined in the std
namespace.
cout
是std
命名空间中定义的全局对象,也是命名空间中定义endl
的(流操作器)函数std
。
If you take no action to import their names into the global namespace, you won't be able to refer to them with the unqualified identifiers cout
and endl
. You have to use the fully qualified names:
如果您不采取任何措施将它们的名称导入全局命名空间,您将无法使用非限定标识符cout
和endl
. 您必须使用完全限定的名称:
std::cout << "Hello, World!" << std::endl;
Basically, what using namespace std
does is to inject all the names of entities that exist in the std
namespace into the global namespace:
基本上,using namespace std
所做的是将std
命名空间中存在的所有实体名称注入到全局命名空间中:
using namespace std;
cout << "Hello, Wordl!" << endl;
However, keep in mind that have such a using
directive in the global namespace is a BADprogramming practice, which will almost certainly lead to evil name clashes.
但是,请记住,using
在全局命名空间中使用这样的指令是一种糟糕的编程实践,这几乎肯定会导致邪恶的名称冲突。
If you reallyneed to use it (e.g. if a function of yours is using many functions defined in the std
namespace, and writing std::
makes the code harder to read), you should rather restrict its scope to the local scope of individual functions:
如果你真的需要使用它(例如,如果你的一个函数使用了std
命名空间中定义的许多函数,并且编写std::
使代码更难阅读),你应该将它的范围限制在单个函数的局部范围内:
void my_function_using_a_lot_of_stuff_from_std()
{
using namespace std;
cout << "Hello, Wordl!" << endl;
// Other instructions using entities from the std namespace...
}
Much better, as long as this is practical, is to use the following, less invasive using declarations, which will selectivelyimport only the names you specify:
更好,只要这是可行的,是使用以下侵入性较小的using declarations,它将选择性地仅导入您指定的名称:
using std::cout;
using std::endl;
cout << "Hello, Wordl!" << endl;
回答by juanchopanza
No!You do not need using namespace std
, and you shouldn't use it. Use fully qualified names std::cout
and std::endl
, or, in a small scope,
不!你并不需要using namespace std
,而你不应该使用它。使用完全限定名称std::cout
和std::endl
,或者,在小范围内,
using std::cout;
using std::endl;
As for the other questions, std::cout
is not a function. It is a kind of global output stream object bound to the standard output. And there isn't an std::cout
in C.
至于其他问题,std::cout
不是函数。它是一种绑定到标准输出的全局输出流对象。并且std::cout
在 C 中没有。
回答by DuncanACoulter
using namespace std;
brings the names in a collection of names (called a namespace) into the current scope. Most textbooks seem to encourage the use as follows:
将名称集合(称为名称空间)中的名称带入当前作用域。大多数教科书似乎鼓励使用如下:
#include <iostream>
using namespace std;
int main()
{
//Code which uses cout, cin, cerr, endl etc.
}
Some people discourage its use in this manner because you could have unexpected collisions with names when namespace scopes overlap and will encourage you to use the fully qualified names like std::endl directly
有些人不鼓励以这种方式使用它,因为当命名空间范围重叠时,您可能会与名称发生意外冲突,并且会鼓励您直接使用像 std::endl 这样的完全限定名称
You have other options such as
您还有其他选择,例如
a) exploiting the scoping rules to temporarily bring in the namespace
a) 利用作用域规则临时引入命名空间
int main()
{
{
using namespace std;
//Code which uses things from std
}
//Code which might collide with the std namespace
}
b) or only bring in the things you need
b) 或者只带你需要的东西
using std::endl;
using std::cin;
In answer to your last question cin is a stream object (a collection of functions and data that supports the stream extraction and insertion operators >> and << )
回答您的最后一个问题 cin 是一个流对象(支持流提取和插入运算符 >> 和 << 的函数和数据的集合)
回答by Silas
cout and endl are members of the standard library in C++. If you want to use them without the using statement, just prepend the namespace:
cout 和 endl 是 C++ 标准库的成员。如果你想在没有 using 语句的情况下使用它们,只需在命名空间之前:
std::cout
std::endl
std::cout
std::endl
This might be of use to you:
这可能对您有用:
http://msdn.microsoft.com/en-us/library/bzbx67e8(VS.80).aspx
http://msdn.microsoft.com/en-us/library/bzbx67e8(VS.80).aspx
cout
does not exist in C.
cout
在 C 中不存在。
回答by Danylo Fitel
Typically, "using namespace std" is declared only in small learning projects, never in real programs. The reason is that you do not need to include everything from that namespace into your code, first of all because it is takes time for compiler to do that. Stroustrup himself writes that this is bad taste. And it is better than printf in C, because it is type-safe and can be easily overloaded for your own types without needing to change the library classes.
通常,“使用命名空间 std”仅在小型学习项目中声明,而不在实际程序中声明。原因是您不需要将该命名空间中的所有内容都包含在您的代码中,首先是因为编译器需要时间来做到这一点。Stroustrup 自己写道,这是不好的品味。它比 C 中的 printf 更好,因为它是类型安全的,并且可以轻松地为您自己的类型重载,而无需更改库类。