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之外,标准库还定义了 ...
随机推荐
- Union and Intersection of two sorted lists 并集和交集
跟面试官确认是arrayList还是singly-linked list /* Union 并集:两个升序的list a, b, 返回其并集(升序排序)*/ public class UnionTw ...
- (四)创建ROS程序包(就是软件包)
你的 ROS 程序包都放到下面这个目录里, 切换到这个目录: $ cd ~/catkin_ws/src 使用下面的命令: 创建一个 ROS 程序包 名字就叫:beginner_tutorials $ ...
- [z]oracle 创建job
https://www.cnblogs.com/lijiasnong/p/3382578.html alter system enable restricted session;--创建表create ...
- Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead.解决方法
今天在eclipse里报这个错误: Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead. Please ...
- msyql [note] mysqld (mysqld 5.6.40) starting as process xxxx...
my.ini有2个配置一个是客户端[client],一个是服务器端[mysqld] 如果把2个都设置为utf8,那么中文会显示乱码,或者无法插入中文 而把客户端设置为gbk,把服务器端设置为utf8就 ...
- Linux安装命令出现如下错误:cannot find a valid baseurl for repo :base/7x86_64
今天刚回到家,在我的虚拟机上有安装了一个Linux系统,安装好之后,想要安装如下命令,yum install wget,yum install gcc,yum install vim,发现一个也没有安 ...
- 5E - A + B Again
There must be many A + B problems in our HDOJ , now a new one is coming. Give you two hexadecimal in ...
- C# 获取 存储过程 返回值
C#获取存储过程的返回值,这一方法,总是容易忘,今天给贴出来,以方便下次使用 存储过程: CREATE PROCEDURE [dbo].[Proc_GetInfo] ), ) out ...
- Tech 2 doesn’t system for H2 above 2007
I purchased my Tech2 from obd2tool.com and it operates excellent. Can not definitely compare softwar ...
- tableView中cell的复用机制
TableView的重用机制,为了做到显示和数据分离,IOS tableView的实现并且不是为每个数据项创建一个tableCell.而是只创建屏幕可显示最大个数的cell,然后重复使用这些cell, ...