【问题标题】:Translate Java code to C++将 Java 代码转换为 C++
【发布时间】:2016-04-14 22:50:13
【问题描述】:

我有 Java 决策树代码可以在 C++ 中传递。 当我尝试构建树时,我不太记得逻辑内部指针。 Java 代码:

public class Node {
Node parent;
Node children[];
List<Instance> instances;
....
};

Node(Node parent, List<Instance> instances) {
        this.parent = parent;
        children = new Node[Instance.FTSVALUERANGE];
        this.instances = instances;
        ....
 }

对于树的生成:

public class ID3 {
Node root;
...

public static Node generate(List<Instance> instances) {
        Node root = new Node(null, instances);
        expand(root, 0);
        return root;
    }
static void expand(Node node, int depth) {
             ...
            ArrayList<ArrayList<Instance>> ts = new ArrayList<ArrayList<Instance>>();
             ...
            /* Grow the tree recursively */
        for (int i = 0; i < Instance.FTSVALUERANGE; i++) {
            if (ts.get(i).size() > 0) {
                node.children[i] = new Node(node, ts.get(i));
                expand(node.children[i], depth + 1);
            }
}}}

这里是我的 c++ 实现;节点:

class Node
{
    public:
        Node();
        Node(Node* parent, std::vector<Instance>& instances);
        Node* parent;
        Node** children;
        std::vector<Instance> instances;

    ....
};

Node::Node()
{
    parent=NULL;
    children=NULL;
}
Node::Node(Node* parent, std::vector<Instance>& instances) {
        this->parent = parent;
        this->children = new Node*[Instance::FTSVALUERANGE];
        this->instances = instances;
      ...
 };

对于树的生成:

class ID3{
  Node* root;
  static void expand(Node* node, int depth);
  static Node* generate(vector<Instance> instances);
  ...
  };
  Node* ID3::generate(vector<Instance> instances) {
    Node* root = new Node(NULL, instances);
    expand(root, 0);// when use ID3.chi_square_100// there is no prunning,
    return root;
 }
 void ID3::expand(Node* node,int depth){
 ...
      for (int i = 0; i < Instance::FTSVALUERANGE; i++) {
            if (ts[i].size() > 0) {
                node->children[i] = new Node(node, ts[i]);
                expand(node->children[i], depth + 1);
            }
    }}}

当我尝试运行一些不适用于儿童的东西时。 完整的实现在这里:https://courses.cs.washington.edu/courses/cse446/12wi/ps/hw4/ID3.java
为了我的目的,我做了一些改变。
提前致谢。
朱塞佩。

【问题讨论】:

  • 为什么要实现自己的数据结构?我建议您使用 Java 和 C++ 中已有的数据结构,或者至少阅读实现,以便了解它们的工作原理。当您不理解它们时从头开始编写它们将浪费大量时间(和错误)如果您的程序中有错误,我建议使用您的调试器来查找它。
  • 不要使用NULL,而是使用nullptr。不要使用指向指针的指针,而使用智能指针,例如unique_ptr,对动态列表使用std::vector
  • 您好@GuillaumeRacicot,感谢您的帮助。 “不要使用指向指针的指针,而是使用诸如 unique_ptr 之类的智能指针”是什么意思?
  • 指向指针的指针是声明为Node** 的变量。它有时用作指向指针的数组(因为指针可以很好地添加为数组),但这通常是一个坏主意,因为它的使用容易出错,而且如果不注意可能会导致内存错误,尤其是对于该语言的新手。在 C++ 中,您有像 std::vector 这样的工具,它是一个动态数组类。
  • 我说使用智能指针是因为在处理手动内存管理时很容易出错。我建议您查看std::unique_ptr 类,它在编译时强制执行内存管理规则,而不是运行时错误,您会得到编译时错误。但我能给你的最好建议是阅读诸如 learncpp.com 之类的教程

标签: java c++ pointers decision-tree code-conversion


【解决方案1】:

感谢 Guillaume Racicot 的建议!
这是代码:
在 Node.cpp 中:

class Node
{
public:
    Node();
    Node(Node* parent, std::vector<Instance>& instances);
    Node* parent;
    std::vector<Node*> children;
    ....
};

Node::Node(Node* parent, std::vector<Instance>& instances) {

    this->parent = parent;
    this->children= std::vector<Node*>(Instance::FTSVALUERANGE);
    ...
}

【讨论】:

    猜你喜欢
    • 2014-05-27
    • 2015-09-27
    • 2014-09-16
    • 2017-02-15
    • 1970-01-01
    • 2013-07-12
    • 2011-12-31
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多