首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在-order中写入txt文件(3 txt文件)

在-order中写入txt文件(3 txt文件)
EN

Stack Overflow用户
提问于 2018-12-07 03:31:27
回答 1查看 108关注 0票数 0

我正在处理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,邮购

所以我需要:

  1. preorder.txt
  2. inorder.txt
  3. postorder.txt

还有这些数字

这是我第一次为糟糕的英语道歉

EN

回答 1

Stack Overflow用户

发布于 2018-12-07 18:34:55

但是我需要用数字创建3 txt文件( preorder.txt,inorder.txt,postorder.txt)

因此,您所指向的代码有一个例子:

代码语言:javascript
复制
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输出,这通常是默认的用户屏幕。

现在考虑一下这个轻量的修改:

代码语言:javascript
复制
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)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53662818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档