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

    MFC Collection classes

    Can anyone tell me the difference between MFC classes CList and CArray ? Which one should be used under what conitions ?


  2. #2
    Join Date
    May 1999
    Location
    Antwerp, Belgium
    Posts
    136

    Re: MFC Collection classes

    If it's not important to have an index on your collection use the CList : inserting a new element to a CList is faster then CArray.

    If you want to access elements with an index then you have to use CArray.

    See also to the vector, list, map classes of STL. They do the same thing as the MFC-classes but they are useable in all kind of applications and not just MFC-applications.


  3. #3
    Guest

    For Mr. Franky Braem

    Thank you for u'r response to CList and CArray question.
    Doesnt CList also have an index variable ? The only difference being that instead of an integer as in CArray, you have a POSITION variable. So u can access CList values also thro POSITION variables.

    In that case, what is the difference now btween those two ?


  4. #4
    Join Date
    May 1999
    Location
    Antwerp, Belgium
    Posts
    136

    Re: For Mr. Franky Braem

    This is the difference :

    In a CList when you want for example the 5th element, you have to make a loop. You can't access it immediately.

    typedef CList<CString, CString &> StringList;

    StringList MyList;
    MyList.AddHead(CString("Element 6"));
    MyList.AddHead(CString("Element 5"));
    MyList.AddHead(CString("Element 4"));
    MyList.AddHead(CString("Element 3"));
    MyList.AddHead(CString("Element 2"));
    MyList.AddHead(CString("Element 1"));
    int i = 0;
    for (POSITION pos = MyList.GetHeadPosition(); pos && i < 4; i++)
    {
    MyList.GetNext(pos);
    }
    // Now pos points to the 5th element
    if ( i == 4 )
    {
    CString s = MyList.GetAt(pos);
    std::cout << (const char *)s;
    }




    If we use CArray, we can use the index of the element. So we don't have to write a loop.


    std::cout << (const char *) s.GetAt(4);




    When it's not important in what order you process your collection, then use CList. If you wan't to access the elements directly then use CArray.


  5. #5
    Guest

    Re: For Mr. Franky Braem

    Thank you for the detailed reply.
    Is that the ONLY difference between the two classes ?


  6. #6
    Join Date
    Apr 1999
    Posts
    383

    Re: For Mr. Franky Braem

    If you want to know all the details, it's all in the online docs: "Collections: Template-Based Classes".

    CArray implements an array-based container - a sequence of elements contiguous in memory. This means you can navigate it forwards and back with a pointer to an element by adding or subtracting the element size. Directly accessing element n is efficient, just involving an offset of n times the element size. This is what the array syntax does, as in: array[n];
    Adding to or deleting from the end of an array is relatively efficient, but inserting into or deleting from the middle is inefficient because other items must be shuffled up or down by one.

    CList implements a list-based container - a linked list of node elements where each node has a pointer to the next node and previous node (this is probably what is returned in the POSITION variable). This means you can navigate back and forward just by following the next and previous node pointers. Directly accessing element n may be inefficient, because it involves n pointer indirections.
    Adding to or deleting from anywhere in a list is efficient because it just involves changing pointers.

    So: Arrays are good for random and sequential access of relatively static contents, lists are good for sequential access of dynamic contents.

    Dave



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