Recommendations for Choosing a Collection Class

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

This article contains detailed information designed to help you choose a collection class for your particular application needs.

Your choice of a collection class depends on a number of factors, including:

  • The features of the class shape: order, indexing, and performance, as shown in the Collection Shape Features table later in this topic

  • Whether the class uses C++ templates

  • Whether the elements stored in the collection can be serialized

  • Whether the elements stored in the collection can be dumped for diagnostics

  • Whether the collection is type-safe

The following table, Collection Shape Features, summarizes the characteristics of the available collection shapes.

  • Columns 2 and 3 describe each shape's ordering and access characteristics. In the table, the term "ordered" means that the order in which items are inserted and deleted determines their order in the collection; it does not mean the items are sorted on their contents. The term "indexed" means that the items in the collection can be retrieved by an integer index, much like items in a typical array.

  • Columns 4 and 5 describe each shape's performance. In applications that require many insertions into the collection, insertion speed might be especially important; for other applications, lookup speed may be more important.

  • Column 6 describes whether each shape allows duplicate elements.

Collection Shape Features

ShapeOrderedIndexedInsert an elementSearch for specified elementDuplicate elements
ListYesNoFastSlowYes
ArrayYesBy intSlowSlowYes
MapNoBy keyFastFastNo (keys) Yes (values)

The following table, Characteristics of MFC Collection Classes, summarizes other important characteristics of specific MFC collection classes as a guide to selection. Your choice may depend on whether the class is based on C++ templates, whether its elements can be serialized via MFC's document serialization mechanism, whether its elements can be dumped via MFC's diagnostic dumping mechanism, or whether the class is type-safe — that is, whether you can guarantee the type of elements stored in and retrieved from a collection based on the class.

Characteristics of MFC Collection Classes

ClassUses C++

templates
Can be

serialized
Can be

dumped
Is

type-safe
CArrayYesYes 1Yes 1No
CByteArrayNoYesYesYes 3
CDWordArrayNoYesYesYes 3
CListYesYes 1Yes 1No
CMapYesYes 1Yes 1No
CMapPtrToPtrNoNoYesNo
CMapPtrToWordNoNoYesNo
CMapStringToObNoYesYesNo
CMapStringToPtrNoNoYesNo
CMapStringToStringNoYesYesYes 3
CMapWordToObNoYesYesNo
CMapWordToPtrNoNoYesNo
CObArrayNoYesYesNo
CObListNoYesYesNo
CPtrArrayNoNoYesNo
CPtrListNoNoYesNo
CStringArrayNoYesYesYes 3
CStringListNoYesYesYes 3
CTypedPtrArrayYesDepends 2YesYes
CTypedPtrListYesDepends 2YesYes
CTypedPtrMapYesDepends 2YesYes
CUIntArrayNoNoYesYes 3
CWordArrayNoYesYesYes 3
  1. To serialize, you must explicitly call the collection object's Serialize function; to dump, you must explicitly call its Dump function. You cannot use the form ar << collObj to serialize or the form dmp << collObj to dump.

  2. Serializability depends on the underlying collection type. For example, if a typed pointer array is based on CObArray, it is serializable; if based on CPtrArray, it is not serializable. In general, the "Ptr" classes cannot be serialized.

  3. If marked Yes in this column, a nontemplate collection class is type-safe provided you use it as intended. For example, if you store bytes in a CByteArray, the array is type-safe. But if you use it to store characters, its type safety is less certain.

Collections
Template-Based Classes
How to: Make a Type-Safe Collection
Accessing All Members of a Collection

Show: