如何存储新建的数据结构?C ++ [英] How do I store my new built data structure? C++

查看:63
本文介绍了如何存储新建的数据结构?C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经获得了很多有关算法和数据结构以及如何将其应用于不同问题的知识.他们说:对问题X使用二叉搜索树(BST)",但从不构造树时做什么.

I have gained lots of knowledge about algorithms and data structures and how to apply them in different problems. They say: "use a binary search tree (BST) on problem X", but never what to do when the tree is constructed.

现在,对于这个问题,我已经在一些具有某些值的对象上构建了BST.此外,搜索功能到位.因此,我编译,插入对象并在字符串上进行搜索.但是,所有这些都在程序终止后被销毁,并且该过程重复进行.我的意图是将树保存在某个位置,并能够根据需要在存储的BST中进行搜索.

Now, for this question, I have built a BST over some objects holding some values. Also, the search function is in place. So, I compile, insert the objects, and do a search on a string. However, all this gets destroyed after the program termination and the process repeats. My intention is to keep the tree stored at some place, and be able to search in the stored BST as I want.

那么,如何将我的BST保留在某个位置,并在已经建立的树上进行搜索?也就是说,我不想每次都建树.我打算建造一棵大树,希望可以选择搜索.

So, how do I keep my BST at some place and do searches on the already built tree? That is, I don't want to built the tree every time. I am planning to built a large tree that I want to have the option to search in.

乏味的示例:

int main() {    
    const string s1 = "Volkswagen";
    const string s2 = "Ferrari";
    const string s3 = "Whatever";
    
    Car* g1 = new Car(s1);
    Car* g2 = new Car(s2);
    Car* g3 = new Car(s3);
    BinTree t; //construct empty tree and add to it in the following 3 lines
    t.insert(g1);
    t.insert(g2);
    t.insert(g3);
    Gene* found_gene; //declare
    found_gene = t.search("Rugbrødsbil") //assign the found object for later use. 
}

推荐答案

如果我正确理解了您想将BST写入磁盘并在程序启动时随时将其加载回去的方法?

If I'm understanding correctly you'd like to write the BST to disk and load it back up anytime the program starts?

C ++实际上并没有内置的方法来执行此操作,因此您需要编写自定义逻辑,以使BST可以对自身进行序列化和反序列化(例如,从字符串中/从字符串中反序列化),然后保存该数据.到磁盘(您可以在C ++中搜索到磁盘I/O),这样程序启动时就可以读回字符串并重新创建树.

C++ doesn't really have a built in way to do this, so you'd need to write custom logic that allows the BST to serialize and deserialize itself (to/from a string for example), and then save that data to disk (you can probably google around for disk I/O in C++), that way when the program starts up it can read the string back and recreate the tree.

https://www.geeksforgeeks.org/serialize-deserialize-binary-树/这是我发现的页面,可以帮助您了解具体内容!

https://www.geeksforgeeks.org/serialize-deserialize-binary-tree/ here is a page I found which may help you out with specifics!

这篇关于如何存储新建的数据结构?C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆