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. f5版本升级

    1)上传系统IOS及Hotfix 点击import按钮,选择要上传的文件.上传成功的话就会显示上传成功的10.2.4的iso文件 2)通过CLI命令行方式上传补丁 通过SSH工具将ISO以及Hotfi ...

  2. sql查询 !='' 和 is not null的区别

    select * from table where a is not null 会把有内容的和内容为空的都查出来而select * from table where a != '' 只会把有内容的查出 ...

  3. linux服务器搭建

    centos7 java web项目环境搭配 2018年07月19日 17:20:21 阅读数:25 首先进行系统安装,此处不进行详细介绍,自行百度安装 一.配置ip地址信息 1.进入/etc/sys ...

  4. ln: operation not permitted

    ln: operation not permitted 在挂载的磁盘上建立软链接提示没有操作权限 例如: ln -s aa bb1ln:aa operation not permitted------ ...

  5. Java08-java语法基础(七)构造方法

    Java08-java语法基础(七)构造方法 一.构造方法 1.什么是构造方法? 构造方法(类方法)是一个方法名和类名相容的特殊的成员方法. 2.构造方法的作用? 当使用new关键字创建一个对象时,为 ...

  6. linux下Redis主从复制

    Redis的主从配置比起MySQL主从配置简单多了,而且Redis主从复制中一个主服务可以有多个从服务,一个从服务又可以有多个从服务. MySQL主从配置http://www.cnblogs.com/ ...

  7. idea自动生成文档注释

    这方面主要分为两块内容,一是利用idea本身具有的生成模板工具进行生成:二是利用第三方插件生成,比如jindent 后期会进行整理更新,待续 下面的网址目前只是关于这方面的介绍,先留存一份而已 htt ...

  8. linux下的压缩命令

    linux zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip - ...

  9. 使用PreparedStatement时,输出完整的SQL语句

    使用psstmt时不能打印出完整的sql语句,挺不方便的,找到一个实现方法,记录下来. package com.zhh.function.util; import java.io.InputStrea ...

  10. 扩展、委托、Lambda、linq

    1.扩展 扩展是一个很有用的功能.如果你有一个类.不能修改,同时你又想给他加一个方法.这个过程就是扩展.扩展就是扩展方法. 例1: 类People public class People { publ ...