1

This is the question :

How to do IT Right ?
IT = add objects dynamically (mean create class structures to support that)

class Branch   
{  
    Leaves lv;         //it should have many leaves!!  
}  

class Tree   
{  
    Branch br;         //it should have many branchs!!!   
}

Now a Non-Working Example (neither is c++ !!, but I try to draw the idea)

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 ++ ;
   }

}

this is for example, reaching a stupid approach:

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
   //...
}

I would like to know the formal normal way for doing this, Thanks!!!!!!

2
  • 4
    I see, you've tagged your question as "vectors". You are very close to solution =) Commented Dec 22, 2009 at 15:27
  • If your looking to implement something like a B-Tree (which has pages of branches), I suggest an array since the page since will most likely stay consistent through the entire tree. The only difference may be the leaf nodes. Commented Dec 22, 2009 at 17:29

2 Answers 2

3

std::vector is the thing, you are looking for, I guess...

class Tree
{
   std::vector<Branch> branches;
};
Sign up to request clarification or add additional context in comments.

3 Comments

It seems I can't avoid std library ! well..at least this would save my code from many many asterisk! =)
@Hernan: I wouldn't say "can't" so much as "shouldn't". 8v)
@Fred you are right, perhaps in microcontrollers world, there is so little of everything that with computers I am a caveman in a city hahaha I will consider std.
1

Vectors are the generic solution. However you shiould look at memory allocation before you start using libraries code such as vectors -- e.g., C++ new, calloc, malloc, thread local memory etc... Each STL container has it's own algorithmic complexities and studying these will aid you in picking the right one

Discussion :

If you want something to grow and you don't have the space for it.... well you have to realloc() or put algorithmically. Acquire a bigger memory buffer and copy the old buffer into it at the correct index offsets. This is what vector does behind the scenes; of-course vector just does this very well by (this is implementation specific) growing using the linear function (2x). growing in this way means it has more memory than it needs, which means that data added to the vector in the future won't cause an immediate reallocation.

However, I must add that this is very inefficient, you can almost always avoid the cost of copying things around. Vector's main use is for contiguous memory regions, you can almost always improve on vector by using linked data structures, perhaps in a binary tree for searching on a key :D

3 Comments

The default shouldn't be "consider calloc/malloc before std::vector", but the otherway around. Always prefer the STL, it was designed and written by far smarter people than you or I.
perhaps you don't consider yourself smart but I have written a lot of low level library code :D. However I do use vector a lot myself as well, when appropriate. If someone asks a question you should aid them in gaining knowledge, not give them an answer on a platter.
This answer will be read by many people, for months to come. Should there not be more knowledge and discussion here than just the correct answer ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.