• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Benjamin

            靜以修身,儉以養(yǎng)德,非澹薄無以明志,非寧靜無以致遠(yuǎn)。
            隨筆 - 397, 文章 - 0, 評(píng)論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            淺析sstream庫

            <sstream>庫定義了三種類:istringstream、ostringstream和stringstream,分別用來進(jìn)行流的輸入、輸出和輸入輸出操作。另外,每個(gè)類都有一個(gè)對(duì)應(yīng)的寬字符集版本。
            <sstream>使用string對(duì)象來代替字符數(shù)組。這樣可以避免緩沖區(qū)溢出的危險(xiǎn)。而且,傳入?yún)?shù)和目標(biāo)對(duì)象的類型被自動(dòng)推導(dǎo)出來,即使使用了不正確的格式化符也沒有危險(xiǎn)。
            istringstream和ostringstream主要用在內(nèi)核格式化中(用cout的ostream方法將格式化信息寫入string對(duì)象中或是讀取string對(duì)象中的格式化信息)例如:
            ostringstream outstr;
            double price= 281.00;
            char* ps = "for a copy of the ISO/EIC C++ standard!";
            outstr << fixed;
            outstr << "Pay only$" << price << ps << end;
            string msg = outstr.str();
            istreamstring允許用istream方法讀取istringsteam對(duì)象中的數(shù)據(jù),也可以用使用string對(duì)象對(duì)istreamsting對(duì)象初始化。簡而言之:istirngstream和ostringstream可以使用
            istream和ostream類的方法來管理存儲(chǔ)在字符串的字符數(shù)據(jù)。
            stringstream通常是用來做數(shù)據(jù)轉(zhuǎn)換的。相比c庫的轉(zhuǎn)換,它更加安全,自動(dòng)和直接。
            例如:#include <string>
            #include <sstream>
            #include <iostream>

            int main()
            {
                std::stringstream stream;
                std::string result;
                int i = 1000;
                stream << i; //將int輸入流
                stream >> result; //從stream中抽取前面插入的int值
                std::cout << result << std::endl; // print the string "1000"
            }

            除了基本類型的轉(zhuǎn)換,也支持char *的轉(zhuǎn)換。

             

            #include <sstream>
            #include <iostream>

            int main()
            {
                std::stringstream stream;
                char result[8] ;
                stream << 8888; //向stream中插入8888
                stream >> result; //抽取stream中的值到result
                std::cout << result << std::endl; // 屏幕顯示 "8888"
            }

            需要注意的是,下面的寫法是不正確的:ifsream fs(Filename);
            stringsteam buff;
            buff << fs.rubf();//這句代碼可以一次性把文件寫入一個(gè)字符串中,然后將Outbuff.str()的值賦給一個(gè)string對(duì)象就可以。
            buff << fs;這樣寫是錯(cuò)誤的,看看下面的<<運(yùn)算符的定義就知道了,它不接受這樣的參數(shù)。
            但可以這樣寫fs>>buf;這樣寫才正確。
            cout << Outbuff << endl;
            這樣寫,編譯器可以通過編譯,但是運(yùn)行后是空值。改成這樣的才行:cout << Outbuff.rubf()<< endl;
            istringstream和ostringstream在文件流的用法和stringstream的用法類似,必須用rubf方法才可以看到內(nèi)容。
            rubf返回的一個(gè)stringbuf 對(duì)象的指針,str方法返回的是一個(gè)string對(duì)象,上面的rubf也可以換成str方法。
            這三個(gè)類的str和rubf的類方法用法都一樣。
            不同的是str方法:有兩個(gè)版本:
            string str()const;//拷貝流緩沖到一個(gè)string對(duì)象中
            void str(constr string& s);//通過流緩沖構(gòu)造一個(gè)string對(duì)象。上面的rubf也可以寫出Outbuff.rubuf()->str(),這樣些效率更高些。

            需要特別注意的是:要清空上面的類對(duì)象的內(nèi)存,不能用clear方法,那只是設(shè)置了錯(cuò)誤標(biāo)志位,要用str("");
            stringstream的<<方法和ostream的 <<方法一樣。
            而且stringstream只有<<運(yùn)算符可以用。
            ostream& operator<< (bool& val );
            ostream& operator<< (short& val );
            ostream& operator<< (unsigned short& val );
            ostream& operator<< (int& val );
            ostream& operator<< (unsigned int& val );
            ostream& operator<< (long& val );
            ostream& operator<< (unsigned long& val );
            ostream& operator<< (float& val );
            ostream& operator<< (double& val );
            ostream& operator<< (long double& val );
            ostream& operator<< (void*& val );
             
            ostream& operator<< (streambuf* sb );
             
            ostream& operator<< (ostream& ( *pf )(ostream&));
            ostream& operator<< (ios& ( *pf )(ios&));
            ostream& operator<< (ios_base& ( *pf )(ios_base&));
            上面的都是它的成員函數(shù),下面的則是全局函數(shù)
            ostream& operator<< (ostream& out, char c );
            ostream& operator<< (ostream& out, signed char c );
            ostream& operator<< (ostream& out, unsigned char c );

            ostream& operator<< (ostream& out, const char* s );
            ostream& operator<< (ostream& out, const signed char* s );
            ostream& operator<< (ostream& out, const unsigned char* s );

            我們還可以利用stringstream來清楚文件內(nèi)容。示例代碼如下:
            ofstream fs(FileName);
            stringstream str;
            str<<fs;
            fs.close();
            這樣文件就被清空了,但是文件還在。

            posted on 2009-03-16 23:51 Benjamin 閱讀(8602) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 泛型編程

            亚洲国产成人久久精品99| 久久久久人妻一区二区三区vr| 久久精品国产免费一区| 国产AV影片久久久久久| 久久九九兔免费精品6| 久久久久亚洲av成人无码电影| 国产精品一区二区久久国产| 热99RE久久精品这里都是精品免费 | 亚洲国产成人久久精品影视| 久久香综合精品久久伊人| 五月丁香综合激情六月久久| 99久久做夜夜爱天天做精品| 潮喷大喷水系列无码久久精品| 国产精品va久久久久久久| 色综合久久久久久久久五月| 久久久久18| 久久人人爽人人精品视频| 熟妇人妻久久中文字幕| 久久精品中文字幕第23页| 久久久精品人妻一区二区三区蜜桃 | 久久精品国产精品亚洲人人| 亚洲国产精品无码久久98| 国产精品久久久天天影视香蕉| 亚洲国产美女精品久久久久∴| 久久久国产精品| 伊人久久精品线影院| 久久超乳爆乳中文字幕| 欧洲人妻丰满av无码久久不卡| 久久婷婷国产剧情内射白浆| 国产91久久综合| 99久久99这里只有免费费精品| 久久亚洲AV无码精品色午夜| 久久精品成人免费国产片小草| 久久99精品国产| 青青国产成人久久91网| 久久精品成人| 久久久久99精品成人片牛牛影视| 久久线看观看精品香蕉国产| 亚洲嫩草影院久久精品| 久久精品国产99久久香蕉| 伊人色综合九久久天天蜜桃|