将Java代码翻译成C++
Translate Java code to C++
我有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
为了我的目的,我做了一些改变
提前谢谢
朱塞佩。
感谢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);
...
}
相关文章:
- 不同翻译单元中不可重载的非内联函数定义
- 需要将一行代码从C++ Qt翻译成PyQt
- 为什么未命名的结构内联变量在每个翻译单元中没有相同的地址?
- 在没有 API 的情况下使用谷歌翻译文本
- 如何执行 cppcheck 交叉翻译单元 (CTU) 静态分析?
- 模板变量是否允许在多个翻译单元中并有效合并?
- 是否允许类在程序中的不同翻译单元之间具有不同的定义?
- 我怎样才能代替使用新的使用malloc翻译
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 如何将 'in' 关键字从 python 翻译成 c++?
- 为什么不同的翻译单元没有独立和沙盒的内存空间?
- 如何用c语言翻译openssl命令pbkdf2
- 为什么编译器不在同一翻译单元中警告 ODR 违规
- 静态变量在同一个翻译单元中被静态方法使用时是否保证被初始化?
- 如何翻译这个给定代码中的 ::* 运算符和member_fn?
- 来自不同翻译单元的函数会干扰吗?
- 在 opengl 中翻译相机时遇到问题
- 如何平均继续翻译向量
- 如何在C++中的不同翻译单元之间共享枚举的实例
- 在Visual Studio中更新Qt翻译(.ts文件)时出现未指定错误