亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c++標(biāo)準(zhǔn)輸入輸出流關(guān)系的前世今生

 更新時(shí)間:2021年04月13日 10:48:09   作者:cpp加油站  
這篇文章主要給大家介紹了關(guān)于c++標(biāo)準(zhǔn)輸入輸出流關(guān)系的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

輸入輸出是每一種編程語言必不可少的部分,c++也不例外,下面我們就來說明c++的標(biāo)準(zhǔn)輸入輸出的前世今生。

1.首先說一下iostream和iostream.h的區(qū)別

#include<iostream>      // 這個(gè)就是1998年標(biāo)準(zhǔn)化以后的標(biāo)準(zhǔn)頭文件,使用時(shí)需要使用聲明命名空間std
#include<iostream.h>        // 這個(gè)就是標(biāo)準(zhǔn)化以前的頭文件,里面的函數(shù)以及類都是全局的

iostream是現(xiàn)在C++中規(guī)定的標(biāo)準(zhǔn),目的在于使C++代碼用于移植和混合嵌入時(shí)不受擴(kuò)展名.h的限制,避免因?yàn)?h而造成的額外的處理和修改。

iostream包含的基本功能和對應(yīng)的iostream.h相同,iostream中定義的內(nèi)容都在命名空間std中,而iostream.h是為了對c語言進(jìn)行兼容,所以將標(biāo)準(zhǔn)輸入輸出功能都定義在全局空間中,他們的使用方法也是不一樣的,另外推薦直接使用iostream,畢竟iostream.h是很多年前的老物件了,標(biāo)準(zhǔn)c++中已經(jīng)明確不適用了,以后有可能被淘汰。

注意:在標(biāo)準(zhǔn)化的過程中,庫中有些部分的細(xì)節(jié)被修改了,所以舊頭文件和新頭文件中的實(shí)體不一定完全對應(yīng)

這里看一下他們使用上的不同:

#include<iostream.h>
或者是 
#include<iostream>
using namespace std;

可見凡是要使用標(biāo)準(zhǔn)c++輸入輸出,都需要加上using namespace std。

2.輸入輸出流關(guān)系梳理

要弄清楚c++的輸入輸出流,必須要從源頭找起,從安裝文件里面找出輸入輸出流相關(guān)的頭文件,大概列一下,相關(guān)頭文件有以下這些:

  • istream,可以看到istream頭文件是聲明了basic_istream模板類
  • ostream,ostream頭文件是聲明了basic_ostream模板類
  • iostream,iostream只是聲明了一個(gè)istream對象和三個(gè)ostream對象,這一點(diǎn)后面會說明
  • iosfwd,iosfwd頭文件里面聲明了所有輸入輸出類的模板類的一個(gè)實(shí)例
  • fstream,fstream里面聲明了basic_filebuf模板類、basic_ifstream模板類、basic_ofstream模板類
  • iomainip,iomainip里面聲明了一些帶參數(shù)的操縱算子
  • sstream,sstream里面聲明了basic_stringbuf模板類、basic_istringstream模板類、basic_ostringstream模板類
  • streambuf,streambuf里面聲明了basic_streambuf模板類

上面說到iosfwd對輸入輸出的類模板做了實(shí)例化,我們截取一段代碼,如下:

  /// Base class for @c char streams.
  typedef basic_ios<char>         ios; //基礎(chǔ)類

  /// Base class for @c char buffers.
  typedef basic_streambuf<char>     streambuf;

  /// Base class for @c char input streams.
  typedef basic_istream<char>         istream;

  /// Base class for @c char output streams.
  typedef basic_ostream<char>         ostream;

  /// Base class for @c char mixed input and output streams.
  typedef basic_iostream<char>         iostream;

  /// Class for @c char memory buffers.
  typedef basic_stringbuf<char>     stringbuf;

  /// Class for @c char input memory streams.
  typedef basic_istringstream<char>     istringstream;

  /// Class for @c char output memory streams.
  typedef basic_ostringstream<char>     ostringstream;

  /// Class for @c char mixed input and output memory streams.
  typedef basic_stringstream<char>     stringstream;

  /// Class for @c char file buffers.
  typedef basic_filebuf<char>         filebuf;

  /// Class for @c char input file streams.
  typedef basic_ifstream<char>         ifstream;

  /// Class for @c char output file streams.
  typedef basic_ofstream<char>         ofstream;

  /// Class for @c char mixed input and output file streams.
  typedef basic_fstream<char>         fstream;

為了敘述方便,后續(xù)我們直接使用以上實(shí)例類來代指模板類,下面用一張圖說明這些類之間的關(guān)系:

箭頭代表繼承的關(guān)系,然后相應(yīng)的buf后綴的類是同一列的其他類使用的緩沖區(qū)類。

以istream,ostream,iostream三者為例,看一下具體的繼承關(guān)系,如下:

template<typename _CharT, typename _Traits>
    class basic_istream : virtual public basic_ios<_CharT, _Traits>;
template<typename _CharT, typename _Traits>
    class basic_ostream : virtual public basic_ios<_CharT, _Traits>;
template<typename _CharT, typename _Traits>
    class basic_iostream
    : public basic_istream<_CharT, _Traits>,
      public basic_ostream<_CharT, _Traits>;

可以看到basic_istream和basic_ostream都是虛繼承于basic_ios,basic_iostream是繼承于basic_istream和basic_ostream,注意這里繼承于basic_ios的時(shí)候之所以要用虛擬繼承,是為了防止多重繼承時(shí),多個(gè)父類共用基類產(chǎn)生二義性。

注:所謂二義性是指basic_iostream類對象會產(chǎn)生兩個(gè)basic_ios對象,用了虛繼承后,就只會產(chǎn)生一個(gè)basic_ios對象,從而避免了二義性。

說到這里,我想問一下,有多少人最開始接觸iostream的時(shí)候首先使用的是cin和cout呢,其實(shí)通過iostream頭文件,我們可以看到,我們常用的cin對象就是istream的一個(gè)實(shí)例,而cout則是ostream的實(shí)例,標(biāo)準(zhǔn)c++中還聲明了ostream的另外兩個(gè)實(shí)例cerr、clog。

總結(jié)

到此這篇關(guān)于c++標(biāo)準(zhǔn)輸入輸出流關(guān)系的文章就介紹到這了,更多相關(guān)c++標(biāo)準(zhǔn)輸入輸出流關(guān)系內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論