我真的很困惑我们如何在头文件中使用std库。何时使用哪个库。"使用命名空间std"和conio.h不同吗??或者它们是一样的。"iostream"和"iostream.h"有什么区别?这些东西让我很困惑.....
-
std
是c++标准库的命名空间,例如string
实际上属于这个命名空间,所以string
的全称是std::string
。using namespace std
告诉编译器我们想要访问这个命名空间中的资源——给我们global
,或者直接访问它保存的string
。查看Pete的评论了解更多细节 -
c++标准库包含许多不同的包,其中一个是
<iostream>
,更多的std
头可以在这里找到:http://www.cplusplus.com/reference/ -
conio.h
看起来像一个旧的dos特定的C头,不再流行了。 -
iostream.h
在某些时候被重命名为iostream
作为标准:http://members.gamedev.net/sicrane/articles/iostream.html
参见:为什么"使用命名空间std"被认为是不好的做法?
编辑感谢Pete!
它只是允许您使用命名空间 std
,这是大多数标准c++头库的命名空间。如果你使用它,你不必在访问时加上std::
前缀,例如,std::cout
或std::cin
,现在分别只是cout
和cin
。
// without using namespace std
#include <iostream>
int main() {
cout << "Hello World"; // error
std::cout << "Hello World"; // outputs Hello World
return 0;
}
现在使用using namespace std
:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World"; // outputs Hello World
return 0;
}
conio.h
是一个c++库,包含getch()
和putch()
等函数。iostream.h
是一个预标准 c++库,在引入命名空间之前使用。iostream
是包含cin
和cout
等对象的标准库。