#include 和 #include 的区别

转帖|其它|编辑:郝浩|2008-07-18 11:37:19.000|阅读 647 次

概述:#include 和 #include 的区别

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  #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: 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:CSDN

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP