前言:
? ? 以前沒有接觸過stringstream這個類的時候,常用的字符串和數字轉換函數就是sscanf和sprintf函數。開始的時候就覺得這兩個函數應經很叼了,但是畢竟是屬于c的。c++中引入了流的概念,通過流來實現字符串和數字的轉換方便多了。在這里,總結之前的,并介紹新學的。
常見格式串:
%% 印出百分比符號,不轉換。
%c 整數轉成對應的 ASCII 字元。
%d 整數轉成十進位。
%f 倍精確度數字轉成浮點數。
%o 整數轉成八進位。
%s 整數轉成字符串。
%x 整數轉成小寫十六進位。
%X 整數轉成大寫十六進位。
%n sscanf(str, "%d%n", &dig, &n),%n表示一共轉換了多少位的字符
sprintf函數
sprintf函數原型為 int sprintf(char *str, const char *format, ...)。作用是格式化字符串,具體功能如下所示:
(1)將數字變量轉換為字符串。
(2)得到整型變量的16進制和8進制字符串。
(3)連接多個字符串。
int main(){char str[256] = { 0 };int data = 1024;//將data轉換為字符串sprintf(str,"%d",data);//獲取data的十六進制sprintf(str,"0x%X",data);//獲取data的八進制sprintf(str,"0%o",data);const char *s1 = "Hello";const char *s2 = "World";//連接字符串s1和s2sprintf(str,"%s %s",s1,s2);cout<<str<<endl; return 0; }
sscanf函數
sscanf函數原型為int sscanf(const char *str, const char *format, ...)。將參數str的字符串根據參數format字符串來轉換并格式化數據,轉換后的結果存于對應的參數內。具體功能如下:
(1)根據格式從字符串中提取數據。如從字符串中取出整數、浮點數和字符串等。
(2)取指定長度的字符串
(3)取到指定字符為止的字符串
(4)取僅包含指定字符集的字符串
(5)取到指定字符集為止的字符串
當然,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; }
輸出結果:123.432 432 11, 即一共轉換了11位的字符。
stringstream類:
<sstream>庫定義了三種類:istringstream、ostringstream和stringstream,分別用來進行流的輸入、輸出和輸入輸出操作。
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.實現任意類型的轉換
template<typename out_type, typename in_value>
out_type convert(const in_value & t){
stringstream stream;
stream<<t;//向流中傳值
out_type result;//這里存儲轉換結果
stream>>result;//向result中寫入值
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
順便說一下,今天做題的時候也用到了stringstream這個類,是二叉樹的序列化和反序列化。
題目鏈接:http://www.lintcode.com/zh-cn/problem/binary-tree-serialization/
二叉樹的序列化和反序列化
設計一個算法,并編寫代碼來序列化和反序列化二叉樹。將樹寫入一個文件被稱為“序列化”,讀取文件后重建同樣的二叉樹被稱為“反序列化”。如何反序列化或序列化二叉樹是沒有限制的,你只需要確保可以將二叉樹序列化為一個字符串,并且可以將字符串反序列化為原來的樹結構。
思路:
通過先序遍歷建立二叉樹的序列化,其中空子樹用'#'來表示。反序列化的時候呢,遇到'#'就停止遞歸構造。另外序列化的時候是將整數通過stringstream轉換成字符串,反序列化是將字符串通過stringstream轉換成整數。
/*** 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;//這里存儲轉換結果stream>>result;//向result中寫入值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 herestring s="";first = true;pre_order(root, s);//先序實現序列化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 hereTreeNode *T = NULL;ss.str("");ss<<data;buildT(T);return T;} };
?