在新的C++标准中,生成新头文件的方法仅仅是将现有C++头文件名中的 .h 去掉。例如,<iostream.h>变成了<iostream>,<complex.h>变成了<complex>,等等。对于C头文件,采用同样的方法,但在每个名字前还要添加一个c。所以C的<string.h>变成了<cstring>,<stdio.h>变成了<cstdio>,等等。
旧的C++头文件是官方所反对使用的(即,明确列出不再支持),但旧的C头文件则没有(以保持对C的兼容性)。
下面是C++头文件的现状:
· 旧的C++头文件名如<iostream.h>将会继续被支持,尽管它们不在官方标准中。这些头文件的内容不在名字空间std中。
· 新的C++头文件如<iostream>包含的基本功能和对应的旧头文件相同,但头文件的内容在名字空间std中。(在标准化的过程中,库中有些部分的细节被修改了,所以旧头文件和新头文件中的实体不一定完全对应。)
· 标准C头文件如<stdio.h>继续被支持。头文件的内容不在std中。
· 具有C库功能的新C++头文件具有如<cstdio>这样的名字。它们提供的内容和相应的旧C头文件相同,只是内容在std中。
#include <iostream> 和 #include <iostream.h>有什么区别?
<iostream>表示你使用的是标准命名空间,也就是在程序开始应该有这么一句话
using namespace std ;
这是遵循c++标准的
相反,"iostream.h" 则没有遵循c++标准 ,这是老式的命名方式 ,延承自C语言。
这是网上摘抄的一相关解释:
其实没有 < iostream.h > 这样的东西 --- 标准化委员会在简化非C标准头文件时用 < iostream > 取代了它。但又没有完全取消 < iostream.h > 的使用,并且很多编译器都同时支持 < iostream > 和 < iostream.h > ,造成现在的局面,老大(标准化委员会)确实有不得已的苦衷。
话说当年,在标准化委员会动手重建新的标准库的时候,遇到了问题。为了避免类名和函数名的冲突问题,引入了名字空间std。但无数现有的C++代码都依赖于使用了多年的伪标准库中的功能,例如,声明在 < iostream.h > 和 < complex.h > 等头文件中的功能。现有软件没有针对使用名字空间而进行相应的设计或者升级,如果用std来包装标准库导致现有代码不能使用,那手底下的小弟(程序员)是不会同意的。
标准化委员会为了拉拢人心,吸引更多的人入会,决定为包装了std的那部分标准库构建新的头文件名。将现有C++头文件名中的.h去掉,所以就出现了 < iostream.h> 和 < iostream > 等很多双胞胎。对于C头文件,采用同样方法但在每个名字前还要添加一个C,所以C的 <string.h> 变成了 <cstring>。
旧的C++头文件是官方明确反对使用的,但旧的C头文件则没有(以保持对C的兼容性)。其实编译器制造商不会停止对客户现有软件提供支持,所以在可以预计的将来,旧的C++头文件还会嚣张一段时间。
如果能明白字符串头文件的使用,举一反三,其他的也差不多会用了。
<string.h> 是旧的C头文件,对应的是基于char*的字符串处理函数;
<string> 是包装了std的C++头文件,对应的是新的strng类;
<cstring> 是对应旧的C头文件的std版本。
跑远了,言归正传。如果你的编译器都同时支持 < iostream > 和 < iostream.h >,那使用 #include < iostream >,得到的是置于名字空间std下的iostream库的元素;如果使用 #include < iostream.h >,得到的是置于全局空间的同样的元素。在全局空间获取元素会导致名字冲突,而设计名字空间的初衷正是用来避免这种名字冲突的发生。还有,打字时 < iostream > 比 < iostream.h > 少两个字,所以我会使用 < iostream
>
另外,我们来看看iostream.h的内容到底是什么:
这是GCC头文件 iostream.h的内容:
view plaincopy to clipboardprint?
// Copyright (C) 1997-1999, 2000 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
#ifndef _BACKWARD_IOSTREAM_H
#define _BACKWARD_IOSTREAM_H 1
#include "backward_warning.h"
#include <iostream> // 看到这里应该更加清晰了吧!
using std::iostream; // 看到这些using声明应该知道为什么可以在使用iostream.h的时候不用using namespace std了吧?
using std::ostream;
using std::istream;
using std::ios;
using std::streambuf;
using std::cout;
using std::cin;
using std::cerr;
using std::clog;
#ifdef _GLIBCXX_USE_WCHAR_T
using std::wcout;
using std::wcin;
using std::wcerr;
using std::wclog;
#endif
using std::ws;
using std::endl;
using std::ends;
using std::flush;
#endif
// Local Variables:
// mode:C++
// End: