C ++如何使一个递归对象的数组像8分支树? [英] C++ how make an Array of recursive Objects like 8-branches tree?

查看:53
本文介绍了C ++如何使一个递归对象的数组像8分支树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Goodmorning,

关于类中的重复,我有点生疏,我已经解决了这个问题,但我只找到二叉树或列表。然后,我在这里寻求帮助。



我必须创建一个立方体类。



类多维数据集有两个成员函数,分析,inizialize。具有8个点和指向8个立方体对象(本身)列表的指针,我称之为子立方体。



功能如下:



1)Inizialize:提供生成子多维数据集的列表,从8个点开始,以及一个表示树级别的整数。输入8个点和一个整数(表示子管的爆炸程度)并输入相对于输入中的8个点的子管的输出列表。



2)分析:验证是保留还是删除当前多维数据集。如果(是),将为当前多维数据集的所有子多维数递归调用此函数。如果(NO)当前立方体从列表中删除。



最初我有一个8个立方体对象的列表,将在其上启动功能分析,最终扩展的那些立方体分为8个子立方体,从当前立方体的8个点开始......依此类推......



我在这个方案中得出的情况,其中P1,..,P8是点,SC是子立方体,C是立方体(实际上立方体abd子立方体是同一个对象):



< img src ='http://img401.imageshack.us/img401/6450/03u1 .jpg'border ='0'/>
上传了 ImageShack.us



我该怎么做?请帮帮我!

解决方案

我不知道你究竟遇到了什么问题。我会将子多维数据集声明为多维数据集指针的向量,例如:



  class  Cube 
{
Initialize( int depth);
...
vector< cube *> m_subCubes;
};



递归调用Initialize函数,将深度-1传播到下一级。如果depth = = 0,则立即返回。否则它会创建子立方体对象并调用Initialize(depth - 1)。



同样适用于Analyze功能。



不要忘记在Cube类的析构函数中删除创建的子多维数据集!


您的递归数据结构是 [ ^ ],是的,你可以使用 std :: vector 作为'对象数组'。


< blockquote>首先,不需要用8个点来表征一个立方体。这是24个自由度,而所有实体3D物体都由6个自由度定义,对于给定的形状(我甚至要解释它吗?),并且,为了定义立方体的形状,你需要一个更多的度数自由,它的大小。因此,7个标量参数完全定义了3D中立方体的大小,方向和位置。你的模型在一开始是错误的(过度的)。



现在,为什么你需要一个子立方体类。如果此对象的语义是多维数据集,则它应该具有相同的类, Cube 。如果子立方体不是立方体,则不应将其称为立方体。再次,你的模型的错误元素。



所以,在讨论你的方法和递归之前(什么是递归对象,顺便说一下?我可以想象一些东西像这样,但你必须定义它),在类实例的容器之前,你需要修复你的模型。然后你需要正确解释你的顾虑。



-SA


Goodmorning,
I'm a bit rusty with regard to the recurrence in the classes, I have serched about this but I only find binary tree or list. Then, I ask here for help.

I have to create a class "cube".

Class cube has two member fuctions, "analyze", "inizialize". Has characterized by 8 points and a pointer to a list of 8 cubes objects (itself) that I call subcubes.

The functions are these:

1) Inizialize: provide to generate list of subcubes, started from 8 points and an integer that indicate level of tree. Take in input 8 points and an integer (that indicates the level of explosion of subcube) and give in output list of subcubes relative to 8 points in input.

2) Analyze: Verify if keep or remove the current cube. If (YES), this function will called recursively for all subcubes of the current cube. If (NO) current cube deleted from list.

Initially I have a list of 8 cube objects, on which will be launched the function analyze, those of interest eventually expanded into 8 subcubes, started from 8 point of current cube..and so on...

I draw into this scheme the situation, where P1, .., P8 are points, SC are subcubes and C are cubes (pratically cube abd subcube are the same object):

<img src='http://img401.imageshack.us/img401/6450/03u1.jpg' border='0'/>
Uploaded with ImageShack.us

How can I do this? Please help me!

解决方案

I don't know what exactly you are having problems with. I would declare the sub-cubes as a vector of pointers to cubes, for example:

class Cube
{
    Initialize (int depth);
    ...
    vector<cube*> m_subCubes;
};


The Initialize function is called recursively, propagating depth - 1 to the next level. If depth is == 0 it returns immediately. Otherwise it creates the sub-cube objects and calls Initialize (depth - 1).

Same for your Analyze function.

Don't forget to delete the created sub-cubes in the destructor of your Cube class!


Your recursive data structure is a tree[^], and yes, you may use std::vector as 'array of objects'.


First of all, there is no need to characterize a cube with 8 points. This is 24 degrees of freedom, while all solid 3D bodies are defined by 6 degrees of freedom, for a given shape (do I even have to explain that?) and, to define the shape of the cube, you need one more degree of freedom, its size. So, 7 scalar parameters fully define the size, orientation and location of a cube in 3D. You model is wrong (excessive) in the very beginning.

Now, why do you need a "sub-cube" class. If the semantic of this object is the cube, it should have the same class, Cube. If the "sub-cube" is not a cube, you should not call it a "cube". Again, wrong element of your model.

So, before discussing your method and "recursion" (what is "recursive object", by the way? I can imagine something like that, but you have to define it), before containers of class instances, you need to fix your model. And then you will need to explain your concerns properly.

—SA


这篇关于C ++如何使一个递归对象的数组像8分支树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆