堅持寫博客——善於總結和記錄學習的每一天
今天簡單的複習了下c++的標準IO庫,c++的IO操作和c的IO操作原理是不一樣的,c++是使用的強大的類庫實現了IO的操作,而c只是使用了簡單的庫函數實現了IO操作。這就帶來了一些本質的區別,首先c的IO操作是不進行類型檢查的,而c++的進行類型檢查的,若輸入的類型不正確,那麼對象的條件狀態會置爲false。
c++的IO操作主要包括三大類:1、iostream類處理面向流的輸入和輸出;2、fstream類處理已命名的文件的IO;3、stringstream類處理內存中的字符串的IO。
其中stringstream類有一個比較有用的地方就是將int等類型的變量的值轉化爲string類型的值
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int val=10;
ostringstream out;
out<<val;
string s=out.str();
cout<<s<<endl;
return 0;
}
其中stringstream的聲明定義在sstream中。strstream和stringstream的區別在於一個是基於c的字符串類型char*,裏一個是基於string進行設計的