简体   繁体   中英

Storing a data structure with pointers to file?

I am trying to store the following Class instance with pointers to a file.

class wavelet_tree {
public:

    std::set<char> alphabet;
    char middle;
    wavelet_tree* Right;
    wavelet_tree* Left;

I can't seem to find a proper way of writing this data to and read from file. Thank you.

Classical recursion walk over the tree can help. Ie something like:

void wavelet_tree_to_stream(std::ostream& to, const wavelet_tree* node) {
   to << "{"
   to << "\"alphabet\":\"" << node->alphabet << "\"";
   to << ',';
   to << "\"middle\": \"" << node->middle << "\"";
   if(nullptr != node->Left) {
      to << ",\"Left\" : ";
      wavelet_tree_to_stream(to, node->Left);
   }
   if(nullptr != node->Right) {
      to << ",\"Right\" : ";
      wavelet_tree_to_stream(to, node->Right);
   }
   to << "}";
}
......
wavelet_tree_to_stream(fstream, root);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM