
Defining a CMap
Microsoft defines a CMap like this:
template< class KEY,
class ARG_KEY, class VALUE, class ARG_VALUE >class CMap : public CObject.
Real helpful, isn’t it? Here's what the four arguments
mean:
-
The first is
how the key is stored within the map.
-
The second is how the key is given to the
map.
-
The third is how the data object is stored in the map.
-
The fourth is how
the data object is passed to the map.
The documentation says that the data is normally stored in the
map and passed on a reference. If you do that then you’ll have to declare an
operator= so that the map can copy the data into the map. Depending upon your
data object’s contents, that can turn into a real nightmare. Alternatively, you
can store the data as a pointer, not as a data object itself. If you do that
then you will have to make very sure that the object that the pointer is
referencing doesn’t get destroyed while the pointer is still considered valid by
the map and, equally important, that the pointer is properly disposed of once
its memory is no longer required.
Traversing a Map
Unlike an array, a map doesn't have an indexing system as such. You have to
traverse the array from one item to the next.
// In this example, cmapIncPaths is
a map of CStrings.
// The key and data are CString references.
CString csKey;
CString csData;
POSITION pos = cmapIncPaths.GetStartPosition();
while ( pos )
{
cmapIncPaths.GetNextAssoc( pos, csKey, csData );
}

|