Collections: Choosing a Collection Class

HomeOverviewHow Do ISampleTutorial

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 following table, Collection Shape Features, summarizes the characteristics of the available collection shapes.

Collection Shape Features


Shape

Ordered?

Indexed?
Insert an element Search for specified element Duplicate elements?
List Yes No Fast Slow Yes
Array Yes By int Slow Slow Yes
Map No By key Fast Fast No (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


Class
Uses C++
templates
Can be
serialized
Can be
dumped
Is
type-safe
CArray Yes Yes 1 Yes 1 No
CByteArray No Yes Yes Yes 3
CDWordArray No Yes Yes Yes 3
CList Yes Yes 1 Yes 1 No
CMap Yes Yes 1 Yes 1 No
CMapPtrToPtr No No Yes No
CMapPtrToWord No No Yes No
CMapStringToOb No Yes Yes No
CMapStringToPtr No No Yes No
CMapStringToString No Yes Yes Yes 3
CMapWordToOb No Yes Yes No
CMapWordToPtr No No Yes No
CObArray No Yes Yes No
CObList No Yes Yes No
CPtrArray No No Yes No
CPtrList No No Yes No
CStringArray No Yes Yes Yes 3
CStringList No Yes Yes Yes 3
CTypedPtrArray Yes Depends 2 Yes Yes
CTypedPtrList Yes Depends 2 Yes Yes
CTypedPtrMap Yes Depends 2 Yes Yes
CUIntArray No No Yes Yes 3
CWordArray No Yes Yes Yes 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.

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