다음을 통해 공유


컬렉션 클래스 선택에 대한 권장 사항

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

모양

Ordered?

Indexed?

Insert an element

Search for specified element

Duplicate elements?

List

아니요

Fast

Slow

배열

By int

Slow

Slow

아니요

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

클래스

Uses C++

템플릿

Can be

serialize됨

Can be

dumped

Is

형식 안전(type-safe)

CArray

Yes 1

Yes 1

아니요

CByteArray

아니요

Yes 3

CDWordArray

아니요

Yes 3

CList

Yes 1

Yes 1

아니요

CMap

Yes 1

Yes 1

아니요

CMapPtrToPtr

아니요

아니요

아니요

CMapPtrToWord

아니요

아니요

아니요

CMapStringToOb

아니요

아니요

CMapStringToPtr

아니요

아니요

아니요

CMapStringToString

아니요

Yes 3

CMapWordToOb

아니요

아니요

CMapWordToPtr

아니요

아니요

아니요

CObArray

아니요

아니요

CObList

아니요

아니요

CPtrArray

아니요

아니요

아니요

CPtrList

아니요

아니요

아니요

CStringArray

아니요

Yes 3

CStringList

아니요

Yes 3

CTypedPtrArray

Depends 2

CTypedPtrList

Depends 2

CTypedPtrMap

Depends 2

CUIntArray

아니요

아니요

Yes 3

CWordArray

아니요

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.

참고 항목

작업

컬렉션의 모든 멤버에 액세스

개념

컬렉션

템플릿 기반 클래스

방법: 형식이 안전한 컬렉션 만들기