共用方式為


CMap Class

A dictionary collection class that maps unique keys to values.

template< class KEY, class ARG_KEY, class VALUE, class ARG_VALUE >class CMap : public CObject

Parameters

  • KEY
    Class of the object used as the key to the map.

  • ARG _ KEY
    Data type used for KEY arguments; usually a reference to KEY.

  • VALUE
    Class of the object stored in the map.

  • ARG _ VALUE
    Data type used for VALUE arguments; usually a reference to VALUE.

Members

Public Structures

Name

Description

CMap::CPair

A nested structure containing a key value and the value of the associated object.

Public Constructors

Name

Description

CMap::CMap

Constructs a collection that maps keys to values.

Public Methods

Name

Description

CMap::GetCount

Returns the number of elements in this map.

CMap::GetHashTableSize

Returns the number of elements in the hash table.

CMap::GetNextAssoc

Gets the next element for iterating.

CMap::GetSize

Returns the number of elements in this map.

CMap::GetStartPosition

Returns the position of the first element.

CMap::InitHashTable

Initializes the hash table and specifies its size.

CMap::IsEmpty

Tests for the empty-map condition (no elements).

CMap::Lookup

Looks up the value mapped to a given key.

CMap::PGetFirstAssoc

Returns a pointer to the first element.

CMap::PGetNextAssoc

Gets a pointer to the next element for iterating.

CMap::PLookup

Returns a pointer to a key whose value matches the specified value.

CMap::RemoveAll

Removes all the elements from this map.

CMap::RemoveKey

Removes an element specified by a key.

CMap::SetAt

Inserts an element into the map; replaces an existing element if a matching key is found.

Public Operators

Name

Description

CMap::operator [ ]

Inserts an element into the map — operator substitution for SetAt.

Remarks

Once you have inserted a key-value pair (element) into the map, you can efficiently retrieve or delete the pair using the key to access it. You can also iterate over all the elements in the map.

A variable of type POSITION is used for alternate access to entries. You can use a POSITION to "remember" an entry and to iterate through the map. You might think that this iteration is sequential by key value; it is not. The sequence of retrieved elements is indeterminate.

Certain member functions of this class call global helper functions that must be customized for most uses of the CMap class. See Collection Class Helpers in the Macros and Globals section of the MFCReference.

CMap overrides CObject::Serialize to support serialization and dumping of its elements. If a map is stored to an archive using Serialize, each map element is serialized in turn. The default implementation of the SerializeElements helper function does a bitwise write. For information about serialization of pointer collection items derived from CObject or other user defined types, see How to: Make a Type-Safe Collection.

If you need a diagnostic dump of the individual elements in the map (the keys and values), you must set the depth of the dump context to 1 or greater.

When a CMap object is deleted, or when its elements are removed, the keys and values both are removed.

Map class derivation is similar to list derivation. See the article Collections for an illustration of the derivation of a special-purpose list class.

Inheritance Hierarchy

CObject

CMap

Requirements

Header: afxtempl.h

See Also

Reference

CObject Class

Hierarchy Chart

Concepts

MFC Sample COLLECT