gpt4 book ai didi

c++ - 将二叉搜索树导出到 .csv 文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:08 27 4
gpt4 key购买 nike

我有一个 BST 数据库,我必须将其导出到一个 .CSV 文件。我可以让它导出第一条记录。我怎样才能让它循环直到整个数据库被导出。

void get(NodeActor* node, ofstream myfile)
{
if(node==NULL)
return;
myfile << "Year , Award, Winner, name, film"<< endl;
myfile << node->year << "," << node->award << "," << node->winner << "," << node->name << "," << node->film<< endl;
get(node->left,myfile);
get(node->right,myfile);
}
void ActorBST::ExportToCSV(NodeActor *node)
{
string fileName;
cout << "File Name(include .csv): ";
cin >>fileName;
ofstream myfile;
myfile.open (fileName);
myfile << "Year , Award, Winner, name, film"<< endl;
myfile << node->year << "," << node->award << "," << node->winner << "," << node->name << "," << node->film<< endl;

myfile.close();
}

最佳答案

我猜你只需要在 ExportToCSV 方法中调用你的 get 函数。
您的 get 方法已经处理递归,因此它应该可以工作,前提是您将顶级节点传递给 ExportToCSV

void ActorBST::ExportToCSV(NodeActor *node)
{
string fileName;
cout << "File Name(include .csv): ";
cin >>fileName;
ofstream myfile;
myfile.open (fileName);
get(node, myfile);
myfile.close();
}

也许您想使 get 成为 ActorBST 类的方法,但这不是强制性的。

关于c++ - 将二叉搜索树导出到 .csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55928383/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com