在新的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: