這是一個問題:如何動態添加對象
如何做到正確?
IT =動態添加對象(指創建類結構,以支持)
class Branch
{
Leaves lv; //it should have many leaves!!
}
class Tree
{
Branch br; //it should have many branchs!!!
}
現在非工作例子(既不是C++ !!,但我儘量畫的想法)
class Branch
{
static lv_count;
Leaves lv; //it should have many leaves!! (and should be some pointer)
public:
add(Leave lv)
{
lv[lv_count] = lv;
lv_count ++ ;
}
}
class Tree
{
static br_count;
Branch br; //it should have many branchs!!! (and should be some pointer)
Tree
public:
add(Branch br)
{
br[br_count] = lv;
br_count ++ ;
}
}
這是例如,達到了一個愚蠢的方法:
class Branch
{
static count;
Leaves l[1000]; //mmm i don't like this
//...
}
class Tree
{
static count;
Branch b[1000]; //mmm i don't like this
//...
}
我想知道這樣做的正規正常的方式,感謝!!!!!!
我看到了,您已將問題標記爲「矢量」 。你很接近解決方案=) – SadSido 2009-12-22 15:27:38
如果你想要實現像B-Tree(有分支頁面)的東西,我建議自頁面以來的一個數組,因爲它很可能在整個樹中保持一致。唯一的區別可能是葉節點。 – 2009-12-22 17:29:48