有关C ++头文件包含的基本问题? [英] Basic question on c++ header file inclusion?

查看:34
本文介绍了有关C ++头文件包含的基本问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下3个程序之间有什么区别?是< iostream>头文件还是C ++标准库?

1.

 #include< iostream>使用命名空间std;int main(){返回0;} 

2.

 #include< iostream>int main(){返回0;} 

3.

 #include&iostream.h>int main(){返回0;} 

谢谢.

解决方案

就所生成的程序而言,两者之间的差异为零-由于该程序未引用 iostream 库中的任何内容,任何智能编译器都不会编译该库.

当您 #include< iostream> 时,您将在头文件中包含 iostream 标准库的声明.使用 #include< iostream.h> 基本上与 #include< iostream> 相同,只是它在全局名称空间中定义了所有库的名称(与之相反)到 std :: 中-可以与使用 iostream 原始版本但未删除 .h .(< iostream.h> 版本通常也不支持宽字符,而仅支持标准的 char .)

使用命名空间std; 表示当前文件中名称引用的默认命名空间将是 std ,这是大多数标准库函数使用的命名空间.虽然这意味着您不必在所有标准库调用之前都加上 std :: 前缀,但这也意味着您必须注意不要定义任何与标准库名称重叠的内容.

What are the differences between below 3 programs ?. Is <iostream> a header file or C++ standard library ?

1.

#include<iostream>
using namespace std;

int main()
{
        return 0;
}

2.

#include<iostream>

int main()
{
        return 0;
}

3.

#include<iostream.h>

int main()
{
        return 0;
}

Thanks in advance.

解决方案

As far as the program that is produced, there is zero difference - since nothing in the iostream library is referenced by the program, none of the library will be compiled in by any intelligent compiler.

When you #include <iostream>, you're including the header file with declarations for the iostream standard library. Using #include <iostream.h> does essentially the same as #include <iostream>, except that it defines all of the library's names within the global namespace as opposed to being in std:: - it's there for reverse-compatibility with programs that used the original version of iostream which didn't drop the .h. (The <iostream.h> versions also often don't support wide characters, but only standard char's.)

using namespace std; means that the default namespace for name references in the current file will be std, which is the namespace used by most standard library functions. While it means you don't have to prefix all of the standard library calls with std::, it also means that you have to be careful not to define anything that overlaps with standard library names.

这篇关于有关C ++头文件包含的基本问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆