C++ iostream 和 iostream.h 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2976477/
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
Difference between iostream and iostream.h
提问by ckv
What is the difference between iostream
and iostream.h
?
iostream
和 和有iostream.h
什么区别?
回答by Brian R. Bondy
iostream.h
is deprecated by those compilers that provide it, iostream
is part of the C++ standard.
iostream.h
被提供它的编译器弃用,iostream
是 C++ 标准的一部分。
To clarify explicitly there is no mention of iostream.h
at all in the current C++ standard (INCITS ISO IEC 14882 2003).
为了明确说明iostream.h
,当前的 C++ 标准 (INCITS ISO IEC 14882 2003) 中根本没有提及。
Edit: As @Jerry mentioned, not only does the current standard not mention it, but no standard for C++ mentions it.
编辑:正如@Jerry 所提到的,不仅当前的标准没有提到它,而且 C++ 的标准也没有提到它。
回答by David Thornley
iostream is a standard header. iostream.h is a non-standard header that was very common in pre-standard C++, and is what iostream evolved from. It's still common to have iostream.h around, presumably for use with older programs.
iostream 是标准头文件。iostream.h 是一个非标准头文件,在标准前的 C++ 中非常常见,并且是 iostream 的演变而来。iostream.h 仍然很常见,大概是为了与旧程序一起使用。
If your implementation have a working copy of iostream.h, it is probably the same as iostream except that everything in iostream is in the std
namespace, while iostream.h generally preceded namespaces, and didn't use them.
如果您的实现有 iostream.h 的工作副本,它可能与 iostream 相同,只是 iostream 中的所有内容都在std
命名空间中,而 iostream.h 通常位于命名空间之前,并且没有使用它们。
If your implementation has both iostream and iostream.h, iostream is likely to work like:
如果您的实现同时具有 iostream 和 iostream.h,那么 iostream 可能会像这样工作:
namespace std
{
#include <iostream.h>
}
although that's not necessarily how it's written.
虽然它不一定是这样写的。
回答by Pushpak Sharma
When C++ was first created, all of the files in the standard runtime library ended in .h. Life was consistent, and it was good. The original version of cout and cin lived in iostream.h. When the language was standardized by the ANSI committee, they decided to move all of the functions in the runtime library into the std namespace (which is generally a good idea). However, this presented a problem: if they moved all the functions into the std namespace, none of the old programs would work any more!
To try to get around this issue, while maintaining backward compatibility for older programs, a new set of header files was introduced that use the same names but lack the .h extension. These new header files have all their functionality inside the std namespace. This way, older programs that include
#include <iostream.h>
do not need to be rewritten, and newer programs can#include <iostream>
.When you include a header file from the standard library, make sure you use the non .h version if it exists. Otherwise you will be using a deprecated version of the header that is no longer supported.
In addition, many of the libraries inherited from C that were still useful in C++ were given a c prefix (e.g. stdlib.h became cstdlib). The functionality from these libraries was also moved into the std namespace to help avoid naming collisions.
However, when you write your own header files, you should give them all a .h extension, since you will not be putting your code in the std namespace.
Rule: use the non .h version of a library if it exists, and access the functionality through the std namespace. If the non .h version does not exist, or you are creating your own headers, use the .h version
首次创建 C++ 时,标准运行时库中的所有文件都以 .h 结尾。生活是一致的,它是好的。cout 和 cin 的原始版本位于 iostream.h 中。当 ANSI 委员会对该语言进行标准化时,他们决定将运行时库中的所有函数移动到 std 命名空间中(这通常是一个好主意)。然而,这带来了一个问题:如果他们将所有函数都移到 std 命名空间中,那么旧的程序将不再起作用!
为了尝试解决这个问题,同时保持旧程序的向后兼容性,引入了一组新的头文件,它们使用相同的名称但缺少 .h 扩展名。这些新的头文件在 std 命名空间中具有所有功能。这样,包含的较旧程序
#include <iostream.h>
不需要重写,而较新的程序可以#include <iostream>
。当您包含标准库中的头文件时,请确保使用非 .h 版本(如果存在)。否则,您将使用不再受支持的已弃用版本的标头。
此外,许多从 C 继承而来的在 C++ 中仍然有用的库被赋予了 ac 前缀(例如 stdlib.h 变成了 cstdlib)。这些库中的功能也被移到 std 命名空间中,以帮助避免命名冲突。
但是,当您编写自己的头文件时,您应该给它们全部添加 .h 扩展名,因为您不会将代码放在 std 命名空间中。
规则:使用库的非 .h 版本(如果存在),并通过 std 命名空间访问功能。如果非 .h 版本不存在,或者您正在创建自己的标头,请使用 .h 版本
Source: https://www.learncpp.com/cpp-tutorial/19-header-files/