istringstream和ostringstream

  从istringstream类中读取数据赋值给某个string,写入某个string到ostringstream类,头文件<sstream>

实例:leetcode297

297. Serialize and Deserialize Binary Tree 序列化和反序列化二叉树
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
or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer
environment.
Design an algorithm to serialize and deserialize a binary tree.There is no restriction on how your serialization / deserialization
algorithm should work.You just need to ensure that a binary tree can be serialized to a string and this string can be
deserialized to the original tree structure.
Example:
You may serialize the following tree :
   1
  /  \
 2   3
/ \
4 5
as "[1,2,3,null,null,4,5]"
Clarification: The above format is the same as how LeetCode serializes a binary tree.You do not necessarily need to follow
this format, so please be creative and come up with different approaches yourself.
Note : Do not use class member / global / static variables to store states.Your serialize and deserialize algorithms should be
stateless.
将一个二叉树的值转化为一个string输出,同时又能把这种string转化二叉树;
前序、中序、后序、层序遍历均可;

法一:
非递归层序遍历以及由层序遍历来构造二叉树;
主要要使用string流,不用string流的话,对于像"[11,-2,3,null,null,42,5]"这样val值大于9或者val值为负数的,你不好处理;

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root)
{
if (root == NULL)
return "#";
ostringstream out;
queue<TreeNode*> que;
que.push(root);
while (!que.empty())
{
TreeNode* cur = que.front();
que.pop();
if (cur)
{
out << to_string(cur->val) << " ";//向ostringstream类out中写入,记住要写空格字符串“ ”
que.push(cur->left);
que.push(cur->right);
}
else
out << "# ";//记住要写空格字符串“ ”
}
return out.str();
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data)
{
if (data == "#")
return NULL;
istringstream in(data);
queue<TreeNode*> que;
string valString;
in >> valString;//从istringstream类in中读取一个string赋值给valString,istringstream类默认以空格为分隔符
TreeNode* root = new TreeNode(stoi(valString));
que.push(root);
while (!que.empty())
{
TreeNode* cur = que.front();
que.pop();
in >> valString;
if (valString == "")
break;
if (valString != "#")
{
cur->left = new TreeNode(stoi(valString));
que.push(cur->left);
}
in >> valString;
if (valString == "")
break;
if (valString != "#")
{
cur->right = new TreeNode(atoi(valString.c_str()));
que.push(cur->right);
}
}
return root;
}
}; // Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

string流的更多相关文章

  1. C++读写TXT文件中的string或者int型数据以及string流的用法

    对文件的读写操作是我们在做项目时经常用到的,在网上看了很多博客,结合自身的项目经验总结了一下,因此写了这篇博客,有些地方可能直接从别的博客中复制过来,但是都会注明出处. 一.文件的输入输出 fstre ...

  2. C++ code:string stream(string流)

    如果有一个文件aaa.txt,有若干行,不知道每行中含有几个整数,要编程输出每行的整数之和,该如何实现? 由于cin>>不能辨别空格与回车的差异,因此只能用getline的方式逐行读入数据 ...

  3. IO库----IO类,文件输入输出,string流

    一.IO类 1.IO库类型和头文件表: 头文件 类型 iostream istream,wistream 从流读取数据 ostream,wostream 向流写入数据 iostream,wiostre ...

  4. string流;

    string流定义在头文件<sstream>中: 可以像标准输入输出流一样,自动判别数据类型输出,遇到空格停止: 定义: stringstream ss:   //定义了一个string流 ...

  5. IO相关3(string流)

    sstream 头文件定义了三个类型来支持内存 IO,这些类型可以向 string 写入数据,从 string 读取数据,就像 string 是一个 IO 流一样. istringstream 从 s ...

  6. Java(45)JDK新特性之String流

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201671.html 博客主页:https://www.cnblogs.com/testero ...

  7. NOJ——聊天止于呵呵(string流重定向+map,水题)

    [1645] 聊天止于呵呵 时间限制: 5000 ms 内存限制: 65535 K 问题描述 (现代版)俗话说:流言止于智者,聊天止于呵呵.输入一段聊天记录,你的任务是数一数有 多少段对话“止于呵呵” ...

  8. C++ 利用流来进行string和其他类的转换

    通过这种方法可以实现任意转换,需要头文件 #include<string> #include<sstream> 期中sstream提供了我们的主角string流,下面给出int ...

  9. 【C++】C++中的流

    目录结构: contents structure [-] 1.IO类 IO对象无拷贝状态 条件状态 文件流 文件模式 string流 1.IO类 除了istream和ostream之外,标准库还定义了 ...

随机推荐

  1. linux命令学习之:ifup/ifdown

    ifup命令网络配置 ifup命令用于激活指定的网络接口.ifdown命令用于禁用指定的网络接口. 实时地手动修改一些网络接口参数,可以利用ifconfig来实现,如果是要直接以配置文件,亦即是在 / ...

  2. php解析优酷网上的视频资源去广告

    1.过程原理解析: 一.准备工作 所谓工欲善其事必先利其器,做好破解的准备工作会令你事半功倍. 1.首先准备一个Http抓包工具,PC上推荐Fiddler或者Postman,iOS上推荐Surge 2 ...

  3. 通过docker-compose构建ghost博客(二)

    上一篇通过yml文件构建 ghost博客,这次通过构建nginx服务,并添加反向代理来运行搭建的ghost博客. 目录结构 ghost.conf 就是 定义的nginx 加载的配置文件 server ...

  4. WEB框架之Ajax

    一 Ajax简介 1 Ajax的介绍 AJAX翻译成中文就是"异步Javascript和XML".即使用JavaScript语言与服务器进行异步交互,传输的数据为XML(当然,传输 ...

  5. 获取URL某个参数

    /* 获取URL某个参数(可以是中文) * 返回:字符串 */ function getUrlParam(key) { // 获取参数 var url = window.location.search ...

  6. better-scroll使用总结

    参考:https://zhuanlan.zhihu.com/p/27407024 better-scroll使用小结 核心就是这4个 <script> import BScroll fro ...

  7. pycharm 出现Process finished with exit code 0 或 Process finished with exit code -1

    Process finished with exit code 0 意味着你的程序正常执行完毕并退出. 可以科普一下exit code,在大部分编程语言中都适用: exit code 0 表示程序执行 ...

  8. 关于document的节点;用Dom2创建节点;

    一.关于节点 1.节点树状图 document>documentElement>body>tagName 2.节点类型 元素节点(标签).文本节点(文本).属性节点(标签属性) 3. ...

  9. js的RegExp

    正则表达式是一种用来匹配字符串的强有力的武器.它的设计思想是用一种描述性的语言来给字符串定义一个规则,凡是符合规则的字符串,我们就认为它“匹配”了,否则,该字符串就是不合法的. 所以我们判断一个字符串 ...

  10. "cni0" already has an IP address different from 10.244.2.1/24。 Error while adding to cni network: failed to allocate for range 0: no IP addresses available in range set: 10.244.2.1-10.244.2.254

    "cni0" already has an IP address different from 10.244.2.1/24. Error while adding to cni n ...