string流

Posted joezzz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了string流相关的知识,希望对你有一定的参考价值。

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流的主要内容,如果未能解决你的问题,请参考以下文章

此应用小部件片段中所有意图 (PendingIntents) 的逻辑流

CSP核心代码片段记录

如何将代码片段存储在 mongodb 中?

是否可以动态编译和执行 C# 代码片段?

从流输入中解析没有根元素的 XML 片段列表

16个必备的JavaScript代码片段