我正在处理ubuntu64位( c++),我有一个二叉树,代码运行良好。但是我需要创建3 txt文件( preorder.txt,inorder.txt,postorder.txt)和运行我的代码的numbers..When --我得到了所有的东西,但都在终端中。我不知道怎么写这三样东西给你看我的剧本.

但我在这里也收到终端机:
vilmos@ubuntu:~$ g++ binfa1.cpp vilmos@ubuntu:~$ ./a.out 10,6,8,11,14,18,预购5,6,8,10,11,14,18,5,6,8,11,18,10,邮购
所以我需要:
还有这些数字
这是我第一次为糟糕的英语道歉
发布于 2018-12-07 18:34:55
但是我需要用数字创建3 txt文件( preorder.txt,inorder.txt,postorder.txt)
因此,您所指向的代码有一个例子:
void btree::inorder_print(){
inorder_print(root);
cout << "\n";
}
void btree::inorder_print(node *leaf){
if(leaf != NULL){
inorder_print(leaf->left);
cout << leaf->value << ",";
inorder_print(leaf->right);
}
}正如我已经猜到的,函数使用cout,所以所有输出都会转到std输出,这通常是默认的用户屏幕。
现在考虑一下这个轻量的修改:
void btree::inorder_print(std::ostream& ostrm = std::cout){
inorder_print(root, ostrm);
ostrm << "\n";
}
void btree::inorder_print(node *leaf, std::ostream& ostrm = std::cout){
if(leaf != NULL){
inorder_print(leaf->left, ostrm);
ostrm << leaf->value << ",";
inorder_print(leaf->right, ostrm);
}
}您的现有代码将像以前一样工作,因为cout是作为默认参数输入的,cout是std::ostream。
要更改输出的位置,只需打开一个ostream到您想要的路径,并将其传递给这些函数。别忘了关门。
概述:修改“工作”代码以接受目标(默认为cout)。
https://stackoverflow.com/questions/53662818
复制相似问题