string和各內置類型的互換方法(stringstream的一種用法)

/*和c的轉換函數比起來用法更爲隱蔽,對初學者來說不夠直觀。*/

#include "iostream"
#include "sstream"
#include "string"
#include "cstdlib"
using namespace std;
int main(void)
{
/*以下是內置類型向string轉換的解決方案*/
 int ival;
 char cval;
 ostringstream out_string;
 string str;
 
 ival = 100;
 cval = 'w';
 out_string << ival << " " << cval;
 str = out_string.str();
 cout << str << endl;

/*以下是string向內置類型轉換的解決方案*/  
 int itmpe;
 char ctmpe;
 str = "100k";
 istringstream in_string( str );
 in_string >> itmpe >> ctmpe;
 cout << itmpe << " " << ctmpe << endl;
 system( "PAUSE" );
 return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章