Chinaunix首页 | 论坛 | 博客
  • 博客访问: 243693
  • 博文数量: 29
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 670
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-19 13:30
文章分类

全部博文(29)

文章存档

2011年(1)

2010年(10)

2009年(1)

2008年(17)

我的朋友

分类: C/C++

2008-07-10 11:30:06

#include 和 #include 有什么区别?

    表示你使用的是标准命名空间,也就是在程序开始应该有这么一句话

    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 变成了

旧的C++头文件是官方明确反对使用的,但旧的C头文件则没有(以保持对C的兼容性)。其实编译器制造商不会停止对客户现有软件提供支持,所以在可以预计的将来,旧的C++头文件还会嚣张一段时间。

    如果能明白字符串头文件的使用,举一反三,其他的也差不多会用了。

    是旧的C头文件,对应的是基于char*的字符串处理函数;

    是包装了stdC++头文件,对应的是新的strng类;

    是对应旧的C头文件的std版本。

    跑远了,言归正传。如果你的编译器都同时支持 < iostream > < iostream.h >,那使用 #include < iostream >,得到的是置于名字空间std下的iostream库的元素;如果使用 #include < iostream.h >,得到的是置于全局空间的同样的元素。在全局空间获取元素会导致名字冲突,而设计名字空间的初衷正是用来避免这种名字冲突的发生。还有,打字时 < iostream > < iostream.h > 少两个字,所以我会使用 < iostream >

 

 

另外,我们来看看iostream.h的内容到底是什么:

这是GCC头文件 iostream.h的内容:

 

  1. // Copyright (C) 1997-1999, 2000 Free Software Foundation, Inc.   
  2.   
  3. //   
  4.   
  5. // This file is part of the GNU ISO C++ Library.  This library is free   
  6.   
  7. // software; you can redistribute it and/or modify it under the   
  8.   
  9. // terms of the GNU General Public License as published by the   
  10.   
  11. // Free Software Foundation; either version 2, or (at your option)   
  12.   
  13. // any later version.   
  14.   
  15.   
  16.   
  17. // This library is distributed in the hope that it will be useful,   
  18.   
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of   
  20.   
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   
  22.   
  23. // GNU General Public License for more details.   
  24.   
  25.   
  26.   
  27. // You should have received a copy of the GNU General Public License along   
  28.   
  29. // with this library; see the file COPYING.  If not, write to the Free   
  30.   
  31. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,   
  32.   
  33. // USA.   
  34.   
  35.   
  36.   
  37. // As a special exception, you may use this file as part of a free software   
  38.   
  39. // library without restriction.  Specifically, if other files instantiate   
  40.   
  41. // templates or use macros or inline functions from this file, or you compile   
  42.   
  43. // this file and link it with other files to produce an executable, this   
  44.   
  45. // file does not by itself cause the resulting executable to be covered by   
  46.   
  47. // the GNU General Public License.  This exception does not however   
  48.   
  49. // invalidate any other reasons why the executable file might be covered by   
  50.   
  51. // the GNU General Public License.  
  52. #ifndef _BACKWARD_IOSTREAM_H  
  53. #define _BACKWARD_IOSTREAM_H 1  
  54. #include "backward_warning.h"  
  55. #include   // 看到这里应该更加清晰了吧!   
  56.   
  57.   
  58.   
  59. using std::iostream; // 看到这些using声明应该知道为什么可以在使用iostream.h的时候不用using namespace std了吧?   
  60.   
  61. using std::ostream;   
  62.   
  63. using std::istream;   
  64.   
  65. using std::ios;   
  66.   
  67. using std::streambuf;   
  68.   
  69.   
  70.   
  71. using std::cout;   
  72.   
  73. using std::cin;   
  74.   
  75. using std::cerr;   
  76.   
  77. using std::clog;  
  78. #ifdef _GLIBCXX_USE_WCHAR_T   
  79.   
  80. using std::wcout;   
  81.   
  82. using std::wcin;   
  83.   
  84. using std::wcerr;   
  85.   
  86. using std::wclog;  
  87. #endif   
  88.   
  89.   
  90.   
  91. using std::ws;   
  92.   
  93. using std::endl;   
  94.   
  95. using std::ends;   
  96.   
  97. using std::flush;  
  98. #endif   
  99.   
  100.   
  101.   
  102. // Local Variables:   
  103.   
  104. // mode:C++   
  105.   
  106. // End:  
阅读(3834) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~