|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
The Microsoft Foundation Class Library provides collection classes to manage groups of objects. These classes are of two types:
Tip The nontemplate collection classes have been provided by MFC beginning with MFC version 1.0. If your code already uses these classes, you can continue to use them. If you write new type-safe collection classes for your own data types, consider using the newer template-based classes.
A collection class is characterized by its "shape" and by the types of its elements. The shape refers to the way the objects are organized and stored by the collection. MFC provides three basic collection shapes: lists, arrays, and maps (also known as dictionaries). You can pick the collection shape most suited to your particular programming problem.
Each of the three provided collection shapes is described briefly below. The table Collection Shape Features in Recommendations for Choosing a Collection Class compares the features of the shapes to help you decide which is best for your program.
The list class provides an ordered, nonindexed list of elements, implemented as a doubly linked list. A list has a "head" and a "tail," and adding or removing elements from the head or tail, or inserting or deleting elements in the middle, is very fast.
The array class provides a dynamically sized, ordered, and integer-indexed array of objects.
- Map (also known as a dictionary)
A map is a collection that associates a key object with a value object.
The easiest way to implement a type-safe collection that contains objects of any type is to use one of the MFC template-based classes. For examples of these classes, see the MFC sample COLLECT.
The following table lists the MFC template-based collection classes.
Collection Template Classes
|Collections of objects of any type||CArray||CList||CMap|
|Collections of pointers to objects of any type||CTypedPtrArray||CTypedPtrList||CTypedPtrMap|
If your application already uses MFC nontemplate classes, you can continue to use them, although for new collections you should consider using the template-based classes. The following table lists the MFC collection classes not based on templates.
Nontemplate Collection Classes
The table Characteristics of MFC Collection Classes in the article Recommendations for Choosing a Collection Class describes the MFC collection classes in terms of their characteristics (other than shape):
- 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
What do you want to do?
General Collection-Class Tasks
- Choose a collection class
- Make a type-safe collection class
- Serialize the elements of a collection
- Use non-template collection classes
- Create a stack collection
- Create a queue collection
- Add data to a collection
Template-Based Collection-Class Tasks
- Use collection classes based on templates
- Use typed-pointer collection class templates
- Implement helper functions for template-based collection classes
Accessing the Members of a Collection (Template-Based or Not)
- Access all members of a collection
- Iterate a collection
- Delete all objects in a CObject collection
- Delete all elements in an array collection class
- Delete all elements in a map collection class
- Delete all elements in a list collection class