string流
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流的更多相关文章
- C++读写TXT文件中的string或者int型数据以及string流的用法
对文件的读写操作是我们在做项目时经常用到的,在网上看了很多博客,结合自身的项目经验总结了一下,因此写了这篇博客,有些地方可能直接从别的博客中复制过来,但是都会注明出处. 一.文件的输入输出 fstre ...
- C++ code:string stream(string流)
如果有一个文件aaa.txt,有若干行,不知道每行中含有几个整数,要编程输出每行的整数之和,该如何实现? 由于cin>>不能辨别空格与回车的差异,因此只能用getline的方式逐行读入数据 ...
- IO库----IO类,文件输入输出,string流
一.IO类 1.IO库类型和头文件表: 头文件 类型 iostream istream,wistream 从流读取数据 ostream,wostream 向流写入数据 iostream,wiostre ...
- string流;
string流定义在头文件<sstream>中: 可以像标准输入输出流一样,自动判别数据类型输出,遇到空格停止: 定义: stringstream ss: //定义了一个string流 ...
- IO相关3(string流)
sstream 头文件定义了三个类型来支持内存 IO,这些类型可以向 string 写入数据,从 string 读取数据,就像 string 是一个 IO 流一样. istringstream 从 s ...
- Java(45)JDK新特性之String流
作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201671.html 博客主页:https://www.cnblogs.com/testero ...
- NOJ——聊天止于呵呵(string流重定向+map,水题)
[1645] 聊天止于呵呵 时间限制: 5000 ms 内存限制: 65535 K 问题描述 (现代版)俗话说:流言止于智者,聊天止于呵呵.输入一段聊天记录,你的任务是数一数有 多少段对话“止于呵呵” ...
- C++ 利用流来进行string和其他类的转换
通过这种方法可以实现任意转换,需要头文件 #include<string> #include<sstream> 期中sstream提供了我们的主角string流,下面给出int ...
- 【C++】C++中的流
目录结构: contents structure [-] 1.IO类 IO对象无拷贝状态 条件状态 文件流 文件模式 string流 1.IO类 除了istream和ostream之外,标准库还定义了 ...
随机推荐
- TZOJ 3305 Hero In Maze II(深搜)
描述 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^.突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了,他急忙赶到迷宫,开 ...
- 客户端无法重新使用 SPID 为 63 的会话,该会话已被重置用于连接
客户端无法重新使用 SPID 为 %d 的会话,该会话已被重置用于连接池.失败 ID 为 %d. 此错误可能是由于先前的操作失败引起的.请查看错误日志,找出在显示此错误消息之前刚发生的失败操作. 20 ...
- swift 快速创建一些基本控件
1.tableview private lazy var cellId = "cellId" fileprivate lazy var tv : UITableView = { l ...
- saltstack 迭代项目到客户端并结合jenkins自动发布多台服务器
前面已经讲解了Webhook实现Push代码后的jenkins自动构建,接下来通过结合slatstack 实现多台机器的项目代码发布. 利用saltstack中file.recurse方法,运用该模块 ...
- wheelView实现滚动选择 三方开源的封装控件 spannableString autofitTextView、PinnedSectionListView SwipeListView等等
wheelView多用于popupwindow用来滚动选择条目 github上的开源三方控件 spannableString autofitTextView.PinnedSectionLi ...
- 定时器NSTimer
/** 添加定时器 */@property (nonatomic, strong) NSTimer *timer; - (void)addTimer{ // 2秒后,自己 调用nextImage方法 ...
- socket错误代码
Socket error 0 - Directly send error Socket error 10004 - Interrupted function call一个封锁操作被对 WSACance ...
- 解决input 有readonly属性 各个浏览器的光标兼容性问题
<input type='text' readonly unselectable='on' onfocus='this.blur()'/> 目标:input 只能读,但是在ie.火狐浏览器 ...
- 线程 day40
操作系统线程理论 线程概念的引入背景 进程 之前我们已经了解了操作系统中进程的概念,程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别 ...
- vue 环境搭建
目前我们的项目前端都采用的是vue js为了方便开发过程中前后端同事进行功能对接,建议每个同事都准备好前后端环境(前端的同事参考文档第二部分,后端同事请参考第一部分),只要保持前后端代码是最新的就可以 ...