#include iostream 和 #include iostream.h 的区别

#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> 是包装了stdC++头文件,对应的是新的strng类;

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

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

 

 

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

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

 

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

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值