码迷,mamicode.com
首页 > 其他好文 > 详细

string流

时间:2018-11-20 11:41:04      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:strong   amp   object   oba   env   int   elf   smi   ati   

istringstream和ostringstream

  istringstream从string读取数据,ostringstream向string写入数据,头文件<sstream>

实例:leetcode297

42、51、58、66、69行

 1 297. Serialize and Deserialize Binary Tree 序列化和反序列化二叉树
 2 Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file 
 3 or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer 
 4 environment.
 5 Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization 
 6 algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be 
 7 deserialized to the original tree structure.
 8 Example: 
 9 You may serialize the following tree:
10     1
11    / 12   2   3
13      / 14     4   5
15 as "[1,2,3,null,null,4,5]"
16 Clarification: The above format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow 
17 this format, so please be creative and come up with different approaches yourself.
18 Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be 
19 stateless.
20 /**
21  * Definition for a binary tree node.
22  * struct TreeNode {
23  *     int val;
24  *     TreeNode *left;
25  *     TreeNode *right;
26  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
27  * };
28  */
29 将一个二叉树的值转化为一个string输出,同时又能把这种string转化二叉树;
30 前序、中序、后序、层序遍历均可;
31 
32 法一:
33 非递归层序遍历以及由层序遍历来构造二叉树;
34 主要要使用string流,不用string流的话,对于像"[11,-2,3,null,null,42,5]"这样val值大于9或者val值为负数的,你不好处理;
35 class Codec {
36 public:
37     // Encodes a tree to a single string.
38     string serialize(TreeNode* root)
39     {
40         if (root == NULL)
41             return "#";
42         ostringstream out;
43         queue<TreeNode*> que;
44         que.push(root);
45         while (!que.empty())
46         {
47             TreeNode* cur = que.front();
48             que.pop();
49             if (cur)
50             {
51                 out << to_string(cur->val) << " ";//向ostringstream类out中写入,记住要写空格字符串“ ”
52                 que.push(cur->left);
53                 que.push(cur->right);
54             }
55             else
56                 out << "# ";//记住要写空格字符串“ ”
57         }
58         return out.str();
59     }
60     
61     // Decodes your encoded data to tree.
62     TreeNode* deserialize(string data)
63     {
64         if (data == "#")
65             return NULL;
66         istringstream in(data);
67         queue<TreeNode*> que;
68         string valString;
69         in >> valString;//从istringstream类in中读取一个string给valString,istringstream类默认以空格为分隔符
70         TreeNode* root = new TreeNode(stoi(valString));
71         que.push(root);
72         while (!que.empty())
73         {
74             TreeNode* cur = que.front();
75             que.pop();
76             in >> valString;
77             if (valString == "")
78                 break;
79             if (valString != "#")
80             {
81                 cur->left = new TreeNode(stoi(valString));
82                 que.push(cur->left);
83             }
84             in >> valString;
85             if (valString == "")
86                 break;
87             if (valString != "#")
88             {
89                 cur->right = new TreeNode(atoi(valString.c_str()));
90                 que.push(cur->right);
91             }
92         }
93         return root;
94     }
95 };
96 
97 // Your Codec object will be instantiated and called as such:
98 // Codec codec;
99 // codec.deserialize(codec.serialize(root));

 

string流

标签:strong   amp   object   oba   env   int   elf   smi   ati   

原文地址:https://www.cnblogs.com/Joezzz/p/9987336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!