CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: List or vectors

  1. #1
    Join Date
    May 2003
    Posts
    47

    List or vectors

    Hi, I Need ur advice about that :

    - Pb :
    I have 2 main objects, class A and class B; There are some other classes which inherit from those 2 : class A1, A2,A3 and B1,B2,B3 ; each type of class should "know" at least one of the other type class(ex : A1 should know B1 (+B2 or B3), B1 should know A2 (+ A1 or A3),......

    - Use:
    1- I need to have 2 "list" of objects : one should be a list of objects which can be an instance of A1,A2,A3 and another one which should be a list of objects which can be an instance of B1,B2,B3
    2-I need to go though all those 2 "lists" (looking for all the objects each time) and do so operations.
    3-Insert or delete some objects when I am doing this.

    -QUestions :
    1- What should I use : List or Vector (of pointers to each type of class (ex : List/Vector of pointers to A1,A2,A3 et List/Vector of pointers to B1, B2,B3).(what is good for memory, time consumming pbs?).
    2- Can I use just one list of class A1,A2,A3 and another one for objects instance of B1,B2,B3 (instead of pointers to those objects) ?
    3- Can I use pointer as a class's member to allow one class to know the others (pointer (member of class A1) point to class B1(+B2),.....and pointer (member of class B2) point to class A1,....?.


    Thanks for your reply,
    PKT

  2. #2
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303
    IMHO, I would say
    1) If you're doing a lot of insertions/deletions in the middle of your 'lists' then a vector would not be a good choice due to large amounts of copying taking place (a deletion in the middle causes subsequent elements to be shuffled up, an insertion causes elements to be shuffled down and possible memory allocation if the vector needs to grow). A list is the better choice.

    2) If you use instances you would need a list for each derived class since they are separate types. By using pointers to the base classes, you only need one list for A and one for B.

    3) Yes, simply store a pointer to the base class as per what is stored in the lists. A pointer to an object of class A will accept a pointer to A1, A2 or A3 and will call virtual functions according to the dynamic typing.

    Regards
    Alan

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Additionally to point 1:

    Insertion into a vector<> (or a deque<> - have you considered that?) invalidates all iterators into that vector, so you can't do insertion half-way through an iteration (unless you're accessing via vector:perator[]() ). Insertion into a list does not invalidate iterators.

    Another consideration: do you need random access to the elements in the container? If so, then you can't use list - try vector or deque.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Feb 2002
    Posts
    5,757
    I recommend STL list container because the program can insert and remove elements at any position.

    As for the last two questions, there are several solutions. One solution is a map of ClassA key and ClassB value.

    Kuphryn

  5. #5
    Join Date
    May 2003
    Posts
    47
    OK thanks , In fact I don't need to access the list in random manner so I am going to use 2 list of pointers . one for each type of class (A with its derivatives A1,A2,A3) and another one for (B with its derivatives B1,B2,B3).

    I just have a last question : about removing a element of one of those list :
    1- I guess that I have to use remove() or erase() but then it would only remove the pointed object (?) and not the pointer too (?) .
    2 - In the pointed object (type B by example) I should have a pointer (pBtoA for ex) to another object of type class A which is pointed by a pointer of list A , and may don't want to remove them ; that's going to append if before I delete the pointed object (of type B), I delete the corresponding pointer pBtoA with "delete pBtoA" : shall I just do pBtoA = NULL ?

    Thanks ,
    PKT

  6. #6
    Join Date
    Feb 2002
    Posts
    5,757
    Okay. The concept to understand is if you created an object via "new," then you must call "delete" when you want to deallocation memory.

    In your design, you are responsible for organizing the pointers and deallocating them when necessary. Thus, if you remove an element from a container and the element comprises of an inner element that points to something else in another container, then it is up to you to update the other container.

    Kuphryn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured