简体   繁体   English

有关C ++头文件包含的基本问题?

[英]Basic question on c++ header file inclusion?

What are the differences between below 3 programs ?. 以下3个程序有什么区别? Is <iostream> a header file or C++ standard library ? <iostream>是头文件还是C ++标准库?

1. 1。

#include<iostream>
using namespace std;

int main()
{
        return 0;
}

2. 2。

#include<iostream>

int main()
{
        return 0;
}

3. 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. 就生成的程序而言,两者之间的差异为零-由于iostream库中的任何内容均未被程序引用,因此任何智能编译器都不会编译该库。

When you #include <iostream> , you're including the header file with declarations for the iostream standard library. #include <iostream> ,您将在头文件中包含iostream标准库的声明。 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 . 使用#include <iostream.h>做本质上是相同#include <iostream> ,除了它定义了全局命名空间内的所有图书馆的名称,而不是在被std:: -它的存在对反向兼容程序,使用了iostream的原始版本,但未删除.h (The <iostream.h> versions also often don't support wide characters, but only standard char 's.) <iostream.h>版本通常也不支持宽字符,而仅支持标准char 。)

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. 表示当前文件中用于名称引用的默认名称空间将是std ,这是大多数标准库函数使用的名称空间。 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. 尽管这意味着您不必在所有标准库调用之前都加上std::前缀,但这也意味着您必须注意不要定义任何与标准库名称重叠的内容。

There is an error in no 3 problem with header file 头文件没有3问题存在错误

The header iostream.h is a non-standard header and does not exist on all platforms. 头iostream.h是非标准头,并非在所有平台上都存在。 As a matter of fact it does not exist on my system (using g++ and the GNU libstdc++). 事实上,它在我的系统上不存在(使用g ++和GNU libstdc ++)。 So any code using it would simply not compile on my system. 因此,任何使用它的代码都将无法在我的系统上编译。

The iostream.h header used to be common before C++ was first standardized in 1998. But since the 98 standard used <iostream> instead of <iostream.h> , the latter has fallen out of favor (being non-standard and all) and is no longer supported on all platforms. iostream.h头文件在1998年C ++首次标准化之前曾经很常见。但是,由于98标准使用<iostream>代替<iostream.h> ,因此后者不再受青睐(成为非标准的和所有的),并且不再在所有平台上受支持。 Code that uses it should be considered non-standard legacy code and is not portable. 使用该代码的代码应被视为非标准旧代码,并且不可移植。 Books that teach it should be considered outdated and avoided. 教它的书应该被认为已经过时并避免使用。

iostream is a header file that provides declarations and prototypes that are an interface to part of the C++ standard library. iostream是一个头文件,提供了声明和原型,它们是C ++标准库一部分的接口。

There's is, on your system, a file called "iostream" (no extension), the contents of which are copied and pasted (with recursive processing of #include s) at the point where you write #include <iostream> . 在您的系统上,有一个名为“ iostream”的文件(无扩展名),该文件的内容在您编写#include <iostream>被复制和粘贴(通过#include的递归处理)。

#include directives always pull in the contents of header files, they never add "libraries". #include指令始终提取头文件的内容,它们从不添加“库”。 Header files often contain declarations and prototypes that are an interface to a library, but the actual libraries themselves are attached to your program by the linker, not the compiler. 头文件通常包含声明和原型,它们是与库的接口,但是实际的库本身是由链接器而不是编译器附加到程序的。 When linking a C++ program, the linker will automatically attach the C++ standard library unless you tell it not to, so you don't have to worry about that. 链接C ++程序时,链接器将自动附加C ++标准库,除非您告知您不这样做,因此您不必为此担心。

Similarly, the using namespace std statement does not do the work of attaching the library. 同样, using namespace std语句不会执行附加库的工作。 This statement only makes it so that you can write, for example, cout or string instead of qualifying them as std::cout and std::string . 该语句仅使您可以编写例如coutstring而不是将它们限定为std::coutstd::string This works for any namespace, but is usually discouraged. 这适用于任何名称空间,但通常不建议这样做。

For the three examples you gave, they all give you the definitions and prototypes you need to use the iostream portion of the C++ standard library, but (2) is preferred, (1) is acceptable, and (3) is deprecated. 对于您给出的三个示例,它们都为您提供了使用C ++标准库的iostream部分所需的定义和原型,但是(2)是首选的,(1)是可接受的,并且(3)已弃用。 (2) gives the additional convenience of being able to omit the std:: prefix (at the cost of reducing the variable names available for you to use yourself), and (3) includes a different file called "iostream.h" instead of "iostream", which is usually the same thing, but the file with the .h is a relic of pre-standard C++ and so may not be supported in future compilers. (2)可以省略std::前缀(以减少您可以使用的变量名为代价)提供了额外的便利,并且(3)包含一个名为“ iostream.h”的文件,而不是“ iostream”通常是相同的东西,但是带有.h的文件是标准C ++的遗物,因此将来的编译器可能不支持。

You first two programs are standard C++ programs whereas the 3rd program uses a non-standard header file <iostream.h> 前两个程序是标准C ++程序,而第三个程序使用非标准头文件<iostream.h>

In 1st program using namespace std brings entire namespace std into scope. 在第一个using namespace std程序中,将整个命名空间std纳入范围。

Have a look at this for more information. 看看这个以获得更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM