為什麼 #include <iostream.h> 過時了

來源:互聯網
上載者:User

原文http://www.cpp-home.com/forum/viewtopic.php?t=252

為什麼#include <iostream.h>過時了

在早些年C++是沒有標準的,所有的編譯器廠商僅僅是遵循“這裡應該有些什麼”這樣一個粗糙的約定。並且你經常由於一些煩人的小小的不同,而無法很容易的把一個程式從一個編譯器/平台移植到另一個。因此C++的爵士們坐到了圓桌前,經過激勵的討論制定出一個叫做ISO-C++的標準。這個標準也被稱為標準C++,C++98 或者更正式ISO/IEC 14882:1998 。

Your Ad Here


在這個標準中很多舊的標頭檔都被標準化了,這樣就可以使用一部固定的手冊而不用擔心你在使用哪一個編譯器了。為了和舊的標頭檔有所不同,新的標頭檔去掉了.h尾碼。因此#include <iostream.h>現在成了#include < iostream>很多其他的標頭檔也是如此。

並且新的標頭檔包含在標準名空間(standard namespace)中。在新的標頭檔中的任何對象,如cout endl 都能在名空間std中找到,也可以被獨立的訪問。

#include <iostream>
void f() {
  std::cout << "BLAH" << std::endl;
  // note: standard library identifiers are to be prefixed with std::
}


或者單獨的從名空間 std 中匯入

#include <iostream>
using std::cout;
void f() {
  cout << "BLAH" << std::endl;
  // note: prefix std:: not neccesary for cout, but for endl
};

或者用下面這條語句匯入名空間std中的所有東西 (推薦寫標頭檔(not recommended for writing header files) )

#include <iostream>
using namespace std;
void f() {
  cout << "BLAH" << endl;
  // note: prefix std:: not needed at all
};

 像平常一樣來使用cout,不用再把std:: 加到前面了。

名空間是非常有用的,但是對於初學者來說並不需要太在意他們,只要在你的includes下面加入using namespace std 就可一個,你現在還不用去為它擔心,這是在你有機會的是閱讀一些關於名空間的資料就可以了。

C++的標準標頭檔是:

  • <algorithm> Many useful algorithms, like std::copy
  • <bitset> Bitset
  • <complex> Complex numbers
  • <deque> Deque containters
  • <exception> Exception handling
  • <fstream> File IO
  • <functional> Function objects
  • <iomanip> IO manipulators
  • <ios> IO base classes
  • <iosfwd> IO forward declarations
  • <iostream> IO streams
  • <istream> Input streams
  • <iterator> Useful iterators, like std::ostream_iterator
  • <limits> Implementation properties
  • <list> List containers
  • <locale> I18 Locales
  • <map> Assiociative containers
  • <memory> Memory managment
  • <new> Dynamic memory management
  • <numeric> Numeric limits
  • <ostream> Output streams
  • <queue> Queue containers
  • <set> Set containers
  • <sstream> String streams
  • <stack> Stack containers
  • <stdexcept> Exception classes
  • <streambuf> Stream buffer classes
  • <string> String classes
  • <typeinfo> Type identification
  • <utility> Utility component
  • <valarray> Arrays of values
  • <vector> Vector containers
  • <cassert> adapted assert.h from C
  • <cctype> adapted ctype.h from C
  • <cerrno> adapted errno.h from C
  • <cfloat> adapted float.h from C
  • <climits> adapted limits.h from C
  • <clocale> adapted locale.h from C
  • <cmath> adapted math.h from C
  • <csetjump> adapted setjump.h from C
  • <csignal> adapted signal.h from C
  • <cstdarg> adapted stdarg.h from C
  • <cstddef> adatped stddef.h from C
  • <cstdio> adapted stdio.h from C
  • <cstdlib> adapted stdlib.h from C
  • <cstring> adapted string.h from C
  • <ctime> adapted time.h from C
  • <cwchar> adapted wchar.h from C
  • <cwctype> adapted wctype.h from C

網上更多的文檔連結:

  • http://www.cppreference.com/ Quite nice, covers the most commoned used things
  • http://www.cplusplus.com/ref/ Covers only few of the headers.
  • http://gcc.gnu.org/onlinedocs/libstdc++/documentation.html Lots of information
  • http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfcpluspluslibraryoverview.asp MS's docu to the VC++ standard libraries
  • http://www.sgi.com/tech/stl/ A lot of info, but remember: SGI STL is only similar to the C++ standard library, it's not the same
  • http://www.dinkumware.com/manuals/reader.aspx?lib=cpp Docs for the Dinkumware implementation of the C++ library

PS:標頭檔的作用沒用翻譯,一是感覺沒有必要;二是也怕自己水平有限,翻譯的不準確,貽笑大方了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.