我们为什么要使用"using namespace std"?我们可以改用 #include< conio.h>吗?



我真的很困惑我们如何在头文件中使用std库。何时使用哪个库。"使用命名空间std"conio.h不同吗??或者它们是一样的。"iostream""iostream.h"有什么区别?这些东西让我很困惑.....

  1. std是c++标准库的命名空间,例如string实际上属于这个命名空间,所以string的全称是std::stringusing namespace std告诉编译器我们想要访问这个命名空间中的资源——给我们global,或者直接访问它保存的string。查看Pete的评论了解更多细节

  2. c++标准库包含许多不同的包,其中一个是<iostream>,更多的std头可以在这里找到:http://www.cplusplus.com/reference/

  3. conio.h看起来像一个旧的dos特定的C头,不再流行了。

  4. iostream.h在某些时候被重命名为iostream作为标准:http://members.gamedev.net/sicrane/articles/iostream.html

参见:为什么"使用命名空间std"被认为是不好的做法?

编辑感谢Pete!

它只是允许您使用命名空间 std,这是大多数标准c++头库的命名空间。如果你使用它,你不必在访问时加上std::前缀,例如,std::coutstd::cin,现在分别只是coutcin

例如:

// 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是包含cincout等对象的标准库。

相关内容

最新更新