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. 服务器使用VMware系软件管理主机集群

    在服务器安装ESXI 6.0系统,此系统300多M,用于管理服务器上的主机. 其他主机安装个vsphere client连接后可ESXI系统可进行简单管理 如果要更强大的功能,需要安装vcenter ...

  2. es数组去重的简写

    console.log([...new Set([2, 2, 12, 1, 2, 1, 6, 12, 13, 6])])

  3. pthreads v3在centos7下的安装与配置

    我的centos版本是7.4.1708,php的版本是7.2.4(注意要是线程安全版),如下图所示: 首先我们在如下网址下载好pthreads的源码: http://pecl.php.net/pack ...

  4. go语言template包中模板语法总结

    package main; import ( "html/template" "os" "fmt" ) type Person struct ...

  5. spingmvc项目根路径访问不到

    问题: 如何改mvc中项目的欢迎页,或者叫做根路径 一个东西快弄完了,就剩下一个问题,应该是个小问题.就是mvc项目的欢迎页,怎么给改下呢 访问根路径http://localhost/demo 怎么都 ...

  6. c#特性attribute:(二)

    日志 初始化 特性类里边构造函数里的属性,带参数不带参数的 ******特性是编译时是不加到il 中的,是加到metadata中 ,本身对程序运行没有影响,除非我们主动的读取和使用区供反射可以使用. ...

  7. SDK Manager 基础下载

    双击SDK Manager打开Android SDK Manager. 全选以下几项 创建新项目 更改gradle的地址: 更改app的build.gradle: android { buildToo ...

  8. 三分钟分布式CAP理论

    分布式系统架构理论,定义了三种指标,理论说我们最多只能满足两个. ## 分布式系统 首先我们这个理论所说的分布式系统,是指系统内会共享数据,互相有连接有交互,才能完成系统功能的的分布式系统.而这个理论 ...

  9. Porsche PIWIS TESTER III

    Allscanner VXDIAG Porsche Piwis III with Lenovo T440P Laptop  Porsche Piwis tester III V37.250.020 N ...

  10. VirtualBox安装android-x86-4.4-r2

    https://jingyan.baidu.com/album/a681b0de1373133b184346cf.html?picindex=10