-
iostream.h
鎖定
標準的輸入輸出流頭文件。用來聲明一些用來在標準輸入輸出設備上進行輸入輸出操作的對象。
- 中文名
- iostream.h
- 外文名
- iostream.h
- 簡 介
- 標準的輸入輸出流頭文件
- 全 稱
- input output stream
- 組 成
- 本類模板
iostream.h含義
iostream.h是input output stream的簡寫,意思為標準的輸入輸出流頭文件。它包含:
(1)cin>>"要輸入的內容"
(2)cout<<"要輸出的內容"
這兩個輸入輸出的方法需要#include<iostream>頭文件來聲明。
iostream.h組成
iostream.h基本類模板
iostream(.h)庫的基礎是類模板的層級結構。類模板以一種與類型無關的方式,提供了這個庫的大部分功能。
基本類模板是一個類模板的集合,其中每個類模板有兩個參數:字符類型(charT)參數決定處理的元素類型,而特性參數對每個特定的元素類型提供一些額外的特徵。
這個類層級結構中的類模板和它們的字符類型的實例相比,名字前面多了前綴basic_。例如,istream由之實例化而來的類模板名為basic_istream,fstream由之實例化而來的類模板名為basic_istream,等等。唯一的例外是ios_base,因為它本身就是類型無關的,所以它並不基於任何類模板,而是一個正規類。
iostream.h類模板實例
iostream(.h)庫中集成了兩組標準的整個iostream類模板層級結構的實例:一組是面向單字節的,處理char類型的元素;另一組是面向寬字節的,處理wchar_t類型的元素。
面向單字節(char型)的實例可能是iostream(.h)庫更為人所知的一部分。ios、istream和ofstream等類都是面向單字節的。右圖是面向單字節的所有類的名稱和關係。
面向寬字節(wchar_t型)的實例的命名規則與面向單字節的實例相同,但所有類和對象名稱前有前綴w,例如wios、wistream和wofstream。
iostream.h標準對象
作為iostream(.h)庫的一部分,頭文件<iostream(.h)>聲明瞭一些用來在標準輸入輸出設備上進行輸入輸出操作的對象。
這些對象分為兩組:面向單字節的,即常見的cin、cout、cerr和clog;其面向寬字節的對應物,聲明為wcin、wcout、wcerr和wclog。
iostream.h類型
iostream(.h)庫中的類很少對其成員的原型使用基本類型,而是通常使用根據其實例的特性定義的類型。對默認的char和wchar_t型的實例,類型streampos、streamoff和streamsize分別用以表示位置、偏移和大小。
iostream.h操縱符
操縱符是用來與對流對象進行操作的插入(<<)和提取(>>)運算符一同使用的全局函數。它們通常變更流的屬性和格式設置。endl、hex和scientific是一些操縱符的例子。
iostream.h比較
iostream.h區別
iostream.h與iostream是不同的。
#include<iostream.h>是在舊的標準C++中使用。在新標準中,用#include<iostream>。iostream的意思是輸入輸出流。#include<iostream>是標準的C++頭文件,任何符合標準的C++開發環境都有這個頭文件。還要注意的是:在VC編程時要添加:
using namespace std;
其原因是:後綴為.h的頭文件C++標準已經明確提出不支持了,早些的實現將標準庫功能定義在全局空間裏,聲明在帶.h後綴的頭文件裏,C++標準為了和C區別開,也為了正確使用命名空間,規定頭文件不使用後綴.h。因此,當使用<iostream.h>時,相當於在C中調用庫函數,使用的是全局命名空間,也就是早期的C++實現;當使用<iostream>的時候,該頭文件沒有定義全局命名空間,必須使用namespace std;這樣才能正確使用cout。
iostream.h關係
<string.h>是舊的C頭文件,對應的是基於char*的字符串處理函數;<string>是包裝了std的C++頭文件,對應的是新的string類;<cstring>是對應舊的C頭文件的std版本。而<iostream.h>和<iostream>的關係,類似於<string.h>和<cstring>的關係,實現的功能是相同的,主要是是否使用命名空間std的區別。
iostream.h使用建議
如果你的編譯器同時支持<iostream>和<iostream.h>,那使用#include<iostream>,得到的是置於命名空間std下的iostream庫的元素;如果使用 #include<iostream.h>,得到的是置於全局空間的同樣的元素。在全局空間獲取元素會導致名字衝突,而設計名字空間的初衷正是用來避免這種名字衝突的發生。還有,打字時<iostream>比<iostream.h>少兩個字,所以通常來説,建議使用<iostream>。
iostream.h內容
/*** *iostream.h - definitions/declarations for iostream classes * * Copyright (c) 1990-1997, Microsoft Corporation. All rights reserved. * *Purpose: * This file defines the classes, values, macros, and functions * used by the iostream classes. * [AT&T C++] * * [Public] * ****/ #if _MSC_VER > 1000 #pragma once #endif #ifdef __cplusplus #ifndef _INC_IOSTREAM #define _INC_IOSTREAM #if !defined(_WIN32) && !defined(_MAC) #error ERROR: Only Mac or Win32 targets supported! #endif #ifdef _MSC_VER // Currently, all MS C compilers for Win32 platforms default to 8 byte // alignment. #pragma pack(push,8) #include <useoldio.h> #endif // _MSC_VER /* Define _CRTIMP */ #ifndef _CRTIMP #ifdef _DLL #define _CRTIMP __declspec(dllimport) #else /* ndef _DLL */ #define _CRTIMP #endif /* _DLL */ #endif /* _CRTIMP */ typedef long streamoff, streampos; #include <ios.h> // Define ios. #include <streamb.h> // Define streambuf. #include <istream.h> // Define istream. #include <ostream.h> // Define ostream. #ifdef _MSC_VER // C4514: "unreferenced inline function has been removed" #pragma warning(disable:4514) // disable C4514 warning // #pragma warning(default:4514) // use this to reenable, if desired #endif // _MSC_VER class _CRTIMP iostream : public istream, public ostream { public: iostream(streambuf*); virtual ~iostream(); protected: iostream(); iostream(const iostream&); inline iostream& operator=(streambuf*); inline iostream& operator=(iostream&); private: iostream(ios&); iostream(istream&); iostream(ostream&); }; inline iostream& iostream::operator=(streambuf* _sb) { istream::operator=(_sb); ostream::operator=(_sb); return *this; } inline iostream& iostream::operator=(iostream& _strm) { return operator=(_strm.rdbuf()); } class _CRTIMP Iostream_init { public: Iostream_init(); Iostream_init(ios &, int =0); // treat as private ~Iostream_init(); }; // used internally // static Iostream_init __iostreaminit; // initializes cin/cout/cerr/clog #ifdef _MSC_VER // Restore previous packing #pragma pack(pop) #endif // _MSC_VER #endif // _INC_IOSTREAM #endif /* __cplusplus */
代碼
- 參考資料