Collections

The newer MFC collection classes use templates, so they can be type-safe yet work for most types. The collection classes that use templates are: CArray, CMap, CList, CTypedPtrArray, CTypedPtrList and CTypedPtrMap. A list of objects, such as a list or array of integers, can easily be created using template classes such as CList and CArray. The following pages show sample uses of various collections.

The following are samples of using the older style collection classes. The MFC documentation suggests to avoid use of these, though. I agree that they should be avoided but it is probably not as much of a problem if you use one of the type-specific ones; they are: CByteArray, CDWordArray, CStringList, CStringArray, CMapStringToString, CWordArray. The simplest collections to use are CStringList and CStringArray.

Finally, I have a sample of Sorting a CArray.


There is an interesting article that describes use of a ported UNIX version of MFC templates (collections). See Bristol Technology - Technical Support - Knowledgebase - Wind/U Topics for the article titled How to Use C++ Templates Across Unix and OpenVMS Platforms.


See my Visual C++ Programmer Stuff page for more C++ stuff.