http://www.cnblogs.com/hujunzheng/p/5042068.html
以前沒(méi)有接觸過(guò)stringstream這個(gè)類(lèi)的時(shí)候,常用的字符串和數(shù)字轉(zhuǎn)換函數(shù)就是sscanf和sPRintf函數(shù)。開(kāi)始的時(shí)候就覺(jué)得這兩個(gè)函數(shù)應(yīng)經(jīng)很叼了,但是畢竟是屬于c的。c++中引入了流的概念,通過(guò)流來(lái)實(shí)現(xiàn)字符串和數(shù)字的轉(zhuǎn)換方便多了。在這里,總結(jié)之前的,并介紹新學(xué)的。
%% 印出百分比符號(hào),不轉(zhuǎn)換。 %c 整數(shù)轉(zhuǎn)成對(duì)應(yīng)的 ASCII 字元。 %d 整數(shù)轉(zhuǎn)成十進(jìn)位。 %f 倍精確度數(shù)字轉(zhuǎn)成浮點(diǎn)數(shù)。 %o 整數(shù)轉(zhuǎn)成八進(jìn)位。 %s 整數(shù)轉(zhuǎn)成字符串。 %x 整數(shù)轉(zhuǎn)成小寫(xiě)十六進(jìn)位。 %X 整數(shù)轉(zhuǎn)成大寫(xiě)十六進(jìn)位。 %n sscanf(str, "%d%n", &dig, &n),%n表示一共轉(zhuǎn)換了多少位的字符
sprintf函數(shù)原型為 int sprintf(char *str, const char *format, ...)。作用是格式化字符串,具體功能如下所示:
(1)將數(shù)字變量轉(zhuǎn)換為字符串。
(2)得到整型變量的16進(jìn)制和8進(jìn)制字符串。
(3)連接多個(gè)字符串。

int main(){ char str[256] = { 0 }; int data = 1024; //將data轉(zhuǎn)換為字符串 sprintf(str,"%d",data); //獲取data的十六進(jìn)制 sprintf(str,"0x%X",data); //獲取data的八進(jìn)制 sprintf(str,"0%o",data); const char *s1 = "Hello"; const char *s2 = "World"; //連接字符串s1和s2 sprintf(str,"%s %s",s1,s2); cout<<str<<endl; return 0;} 
vsscanf函數(shù)
sscanf函數(shù)原型為int sscanf(const char *str, const char *format, ...)。將參數(shù)str的字符串根據(jù)參數(shù)format字符串來(lái)轉(zhuǎn)換并格式化數(shù)據(jù),轉(zhuǎn)換后的結(jié)果存于對(duì)應(yīng)的參數(shù)內(nèi)。具體功能如下:
(1)根據(jù)格式從字符串中提取數(shù)據(jù)。如從字符串中取出整數(shù)、浮點(diǎn)數(shù)和字符串等。
(2)取指定長(zhǎng)度的字符串
(3)取到指定字符為止的字符串
(4)取僅包含指定字符集的字符串
(5)取到指定字符集為止的字符串
當(dāng)然,sscanf可以支持格式串"%[]"形式的,有興趣的可以研究一下。

int main(){ char s[15] = "123.432,432"; int n; double f1; int f2; sscanf(s, "%lf,%d%n", &f1, &f2, &n); cout<<f1<<" "<<f2<<" "<<n; return 0;} 
輸出結(jié)果:123.432 432 11, 即一共轉(zhuǎn)換了11位的字符。
vstringstream類(lèi):
<sstream>庫(kù)定義了三種類(lèi):istringstream、ostringstream和stringstream,分別用來(lái)進(jìn)行流的輸入、輸出和輸入輸出操作。
1.stringstream::str(); returns a string object with a copy of the current contents of the stream.
2.stringstream::str (const string& s); sets s as the contents of the stream, discarding any previous contents.
3.stringstream清空,stringstream s; s.str("");
4.實(shí)現(xiàn)任意類(lèi)型的轉(zhuǎn)換
template<typename out_type, typename in_value> out_type convert(const in_value & t){ stringstream stream; stream<<t;//向流中傳值 out_type result;//這里存儲(chǔ)轉(zhuǎn)換結(jié)果 stream>>result;//向result中寫(xiě)入值 return result; }

int main(){ string s = "1 23 # 4"; stringstream ss; ss<<s; while(ss>>s){ cout<<s<<endl; int val = convert<int>(s); cout<<val<<endl; } return 0;}
輸出:1 1 23 23 # 0 4 4
順便說(shuō)一下,今天做題的時(shí)候也用到了stringstream這個(gè)類(lèi),是二叉樹(shù)的序列化和反序列化。
題目鏈接:http://www.lintcode.com/zh-cn/problem/binary-tree-serialization/
v二叉樹(shù)的序列化和反序列化
設(shè)計(jì)一個(gè)算法,并編寫(xiě)代碼來(lái)序列化和反序列化二叉樹(shù)。將樹(shù)寫(xiě)入一個(gè)文件被稱為“序列化”,讀取文件后重建同樣的二叉樹(shù)被稱為“反序列化”。如何反序列化或序列化二叉樹(shù)是沒(méi)有限制的,你只需要確保可以將二叉樹(shù)序列化為一個(gè)字符串,并且可以將字符串反序列化為原來(lái)的樹(shù)結(jié)構(gòu)。
v思路:
通過(guò)先序遍歷建立二叉樹(shù)的序列化,其中空子樹(shù)用'#'來(lái)表示。反序列化的時(shí)候呢,遇到'#'就停止遞歸構(gòu)造。另外序列化的時(shí)候是將整數(shù)通過(guò)stringstream轉(zhuǎn)換成字符串,反序列化是將字符串通過(guò)stringstream轉(zhuǎn)換成整數(shù)。

/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */class Solution {public: /** * This method will be invoked first, you should design your own algorithm * to serialize a binary tree which denote by a root node to a string which * can be easily deserialized by your own "deserialize" method later. */ bool first; template<typename out_type, typename in_value> out_type convert(const in_value & t){ stringstream stream; stream<<t;//向流中傳值 out_type result;//這里存儲(chǔ)轉(zhuǎn)換結(jié)果 stream>>result;//向result中寫(xiě)入值 return result; } void pre_order(TreeNode *root, string &s){ if(root){ string tmp = convert<string>(root->val); if(!first) s+= " "+tmp; else { first = false; s+=tmp; } pre_order(root->left, s); pre_order(root->right, s); } else { if(first) s+='#'; else { first = false; s+=" #"; } } } string serialize(TreeNode *root) { // write your code here string s=""; first = true; pre_order(root, s);//先序?qū)崿F(xiàn)序列化 return s; } stringstream ss; void buildT(TreeNode * &T){ string s; ss>>s; if(s == "#") return ; int val = convert<int>(s); T = new TreeNode(val); buildT(T->left); buildT(T->right); } /** * This method will be invoked second, the argument data is what exactly * you serialized at method "serialize", that means the data is not given by * system, it's given by your own serialize method. So the format of data is * designed by yourself, and deserialize it here as you serialize it in * "serialize" method. */ TreeNode *deserialize(string data) { // write your code here TreeNode *T = NULL; ss.str(""); ss<<data; buildT(T); return T; }};
![]() | 版權(quán)聲明:本文原創(chuàng)發(fā)表于博客園,作者為小眼兒。 本文歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則視為侵權(quán)。 工作之余: lintcode簡(jiǎn)單題目解析lintcode中等題目解析 |
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注