April 27th, 1999, 01:25 AM
Can anyone tell me the difference between MFC classes CList and CArray ? Which one should be used under what conitions ?
|
Click to See Complete Forum and Search --> : MFC Collection classes April 27th, 1999, 01:25 AM Can anyone tell me the difference between MFC classes CList and CArray ? Which one should be used under what conitions ? Franky Braem April 27th, 1999, 02:22 AM 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. April 27th, 1999, 03:19 AM 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 ? Franky Braem April 27th, 1999, 04:57 AM 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. April 27th, 1999, 05:15 AM Thank you for the detailed reply. Is that the ONLY difference between the two classes ? Dave Lorde April 27th, 1999, 08:52 AM 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 codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |