c++ stringstream(老好用了)

前言:

    之前沒有接觸過stringstream這個類的時候,經常使用的字符串和數字轉換函數就是sscanf和sprintf函數。開始的時候就以爲這兩個函數應經很叼了,可是畢竟是屬於c的。c++中引入了流的概念,經過流來實現字符串和數字的轉換方便多了。在這裏,總結以前的,並介紹新學的。node

常見格式串:  

  %% 印出百分比符號,不轉換。
  %c 整數轉成對應的 ASCII 字元。
  %d 整數轉成十進位。
  %f 倍精確度數字轉成浮點數。
  %o 整數轉成八進位。
  %s 整數轉成字符串。
  %x 整數轉成小寫十六進位。
  %X 整數轉成大寫十六進位。
  %n sscanf(str, "%d%n", &dig, &n),%n表示一共轉換了多少位的字符c++

sprintf函數

   sprintf函數原型爲 int sprintf(char *str, const char *format, ...)。做用是格式化字符串,具體功能以下所示:算法

  (1)將數字變量轉換爲字符串。函數

  (2)獲得整型變量的16進制和8進制字符串。ui

  (3)鏈接多個字符串。this

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和s2
    sprintf(str,"%s %s",s1,s2);
    cout<<str<<endl; 
    return 0;
} 

sscanf函數

  sscanf函數原型爲int sscanf(const char *str, const char *format, ...)。將參數str的字符串根據參數format字符串來轉換並格式化數據,轉換後的結果存於對應的參數內。具體功能以下:spa

  (1)根據格式從字符串中提取數據。如從字符串中取出整數、浮點數和字符串等。設計

  (2)取指定長度的字符串code

  (3)取到指定字符爲止的字符串orm

  (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 here
        string 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 here
        TreeNode *T = NULL;
        ss.str("");
        ss<<data;
        buildT(T);
        return T;
    }
};
相關文章
相關標籤/搜索