description | title | ms.date | f1_keywords | helpviewer_keywords | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: CMapStringToOb Class |
CMapStringToOb Class |
11/04/2016 |
|
|
A dictionary collection class that maps unique CString
objects to CObject
pointers.
class CMapStringToOb : public CObject
Name | Description |
---|---|
CMapStringToOb::CMapStringToOb |
Constructor. |
Name | Description |
---|---|
CMapStringToOb::GetCount |
Returns the number of elements in this map. |
CMapStringToOb::GetHashTableSize |
Determines the current number of elements in the hash table. |
CMapStringToOb::GetNextAssoc |
Gets the next element for iterating. |
CMapStringToOb::GetSize |
Returns the number of elements in this map. |
CMapStringToOb::GetStartPosition |
Returns the position of the first element. |
CMapStringToOb::HashKey |
Calculates the hash value of a specified key. |
CMapStringToOb::InitHashTable |
Initializes the hash table. |
CMapStringToOb::IsEmpty |
Tests for the empty-map condition (no elements). |
CMapStringToOb::Lookup |
Looks up a void pointer based on the void pointer key. The pointer value, not the entity it points to, is used for the key comparison. |
CMapStringToOb::LookupKey |
Returns a reference to the key associated with the specified key value. |
CMapStringToOb::RemoveAll |
Removes all the elements from this map. |
CMapStringToOb::RemoveKey |
Removes an element specified by a key. |
CMapStringToOb::SetAt |
Inserts an element into the map; replaces an existing element if a matching key is found. |
Name | Description |
---|---|
CMapStringToOb::operator [ ] |
Inserts an element into the map — operator substitution for SetAt . |
Once you've inserted a CString
- CObject*
pair (element) into the map, you can efficiently retrieve or delete the pair using a string or a CString
value as a key. You can also iterate over all the elements in the map.
A variable of type POSITION
is used for alternate entry access in all map variations. 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 isn't. The sequence of retrieved elements is indeterminate.
CMapStringToOb
incorporates the IMPLEMENT_SERIAL
macro to support serialization and dumping of its elements. Each element is serialized in turn if a map is stored to an archive, either with the overloaded insertion ( <<
) operator or with the Serialize
member function.
If you need a diagnostic dump of the individual elements in the map (the CString
value and the CObject
contents), you must set the depth of the dump context to 1 or greater.
When a CMapStringToOb
object is deleted, or when its elements are removed, the CString
objects and the CObject
pointers are removed. The objects referenced by the CObject
pointers aren't destroyed.
Map class derivation is similar to list derivation. See the article Collections for an illustration of the derivation of a special-purpose list class.
CMapStringToOb
Header: afxcoll.h
Constructs an empty CString
-to- CObject*
map.
CMapStringToOb(INT_PTR nBlockSize = 10);
nBlockSize
Specifies the memory-allocation granularity for extending the map.
As the map grows, memory is allocated in units of nBlockSize
entries.
The following table shows other member functions that are similar to CMapStringToOb:: CMapStringToOb
.
Class | Member Function |
---|---|
CMapPtrToPtr |
CMapPtrToPtr( INT_PTR nBlockSize = 10 ); |
CMapPtrToWord |
CMapPtrToWord( INT_PTR nBlockSize = 10 ); |
CMapStringToPtr |
CMapStringToPtr( INT_PTR nBlockSize = 10 ); |
CMapStringToString |
CMapStringToString( INT_PTR nBlockSize = 10 ); |
CMapWordToOb |
CMapWordToOb( INT_PTR nBlockSize = 10 ); |
CMapWordToPtr |
MapWordToPtr( INT_PTR nBlockSize = 10 ); |
[!code-cppNVC_MFCCollections#63]
See CObList::CObList
for a listing of the CAge
class used in all collection examples.
Determines how many elements are in the map.
INT_PTR GetCount() const;
The number of elements in this map.
The following table shows other member functions that are similar to CMapStringToOb::GetCount
.
Class | Member Function |
---|---|
CMapPtrToPtr |
INT_PTR GetCount() const; |
CMapPtrToWord |
INT_PTR GetCount() const; |
CMapStringToPtr |
INT_PTR GetCount() const; |
CMapStringToString |
INT_PTR GetCount() const; |
CMapWordToOb |
INT_PTR GetCount() const; |
CMapWordToPtr |
INT_PTR GetCount() const; |
See CObList::CObList
for a listing of the CAge
class used in all collection examples.
[!code-cppNVC_MFCCollections#64]
Determines the current number of elements in the hash table.
UINT GetHashTableSize() const;
Returns the number of elements in the hash table.
The following table shows other member functions that are similar to CMapStringToOb::GetHashTableSize
.
Class | Member Function |
---|---|
CMapPtrToPtr |
UINT GetHashTableSize() const; |
CMapPtrToWord |
UINT GetHashTableSize() const; |
CMapStringToPtr |
UINT GetHashTableSize() const; |
CMapStringToString |
UINT GetHashTableSize() const; |
CMapWordToOb |
UINT GetHashTableSize() const; |
CMapWordToPtr |
UINT GetHashTableSize() const; |
Retrieves the map element at rNextPosition
, then updates rNextPosition
to refer to the next element in the map.
void GetNextAssoc(
POSITION& rNextPosition,
CString& rKey,
CObject*& rValue) const;
rNextPosition
Specifies a reference to a POSITION value returned by a previous GetNextAssoc
or GetStartPosition
call.
rKey
Specifies the returned key of the retrieved element (a string).
rValue
Specifies the returned value of the retrieved element (a CObject
pointer). See Remarks for more about this parameter.
This function is most useful for iterating through all the elements in the map. Note that the position sequence isn't necessarily the same as the key value sequence.
If the retrieved element is the last in the map, then the new value of rNextPosition
is set to NULL
.
For the rValue
parameter, be sure to cast your object type to CObject*&
, which is what the compiler requires, as shown in the following example:
[!code-cppNVC_MFCCollections#65]
This isn't true of GetNextAssoc
for maps based on templates.
The following table shows other member functions that are similar to CMapStringToOb::GetNextAssoc
.
Class | Member Function |
---|---|
CMapPtrToPtr | void GetNextAssoc( POSITION& rNextPosition , void*& rKey , void*& rValue ) const; |
CMapPtrToWord | void GetNextAssoc( POSITION& rNextPosition , void*& rKey , WORD& rValue ) const; |
CMapStringToPtr | void GetNextAssoc( POSITION& rNextPosition , CString& rKey , void*& rValue ) const; |
CMapStringToString | void GetNextAssoc( POSITION& rNextPosition , CString& rKey , CString& rValue ) const; |
CMapWordToOb | void GetNextAssoc( POSITION& rNextPosition , WORD& rKey , CObject*& rValue ) const; |
CMapWordToPtr | void GetNextAssoc( POSITION& rNextPosition , WORD& rKey , void*& rValue ) const; |
See CObList::CObList
for a listing of the CAge
class used in all collection examples.
[!code-cppNVC_MFCCollections#66]
The results from this program are as follows:
Lisa : a CAge at $4724 11
Marge : a CAge at $47A8 35
Homer : a CAge at $4766 36
Bart : a CAge at $45D4 13
Returns the number of map elements.
INT_PTR GetSize() const;
The number of items in the map.
Call this method to retrieve the number of elements in the map.
The following table shows other member functions that are similar to CMapStringToOb::GetSize
.
Class | Member Function |
---|---|
CMapPtrToPtr |
INT_PTR GetSize() const; |
CMapPtrToWord |
INT_PTR GetSize() const; |
CMapStringToPtr |
INT_PTR GetSize() const; |
CMapStringToString |
INT_PTR GetSize() const; |
CMapWordToOb |
INT_PTR GetSize() const; |
CMapWordToPtr |
INT_PTR GetSize() const; |
[!code-cppNVC_MFCCollections#67]
Starts a map iteration by returning a POSITION
value that can be passed to a GetNextAssoc
call.
POSITION GetStartPosition() const;
A POSITION
value that indicates a starting position for iterating the map; or NULL
if the map is empty.
The iteration sequence isn't predictable; therefore, the "first element in the map" has no special significance.
The following table shows other member functions that are similar to CMapStringToOb::GetStartPosition
.
Class | Member Function |
---|---|
CMapPtrToPtr |
POSITION GetStartPosition() const; |
CMapPtrToWord |
POSITION GetStartPosition() const; |
CMapStringToPtr |
POSITION GetStartPosition() const; |
CMapStringToString |
POSITION GetStartPosition() const; |
CMapWordToOb |
POSITION GetStartPosition() const; |
CMapWordToPtr |
POSITION GetStartPosition() const; |
See the example for CMapStringToOb::GetNextAssoc
.
Calculates the hash value of a specified key.
UINT HashKey(LPCTSTR key) const;
key
The key whose hash value is to be calculated.
The Key's hash value
The following table shows other member functions that are similar to CMapStringToOb::HashKey
.
Class | Member Function |
---|---|
CMapPtrToPtr |
UINT HashKey( void *key ) const; |
CMapPtrToWord |
UINT HashKey( void *key ) const; |
CMapStringToString |
UINT HashKey( LPCTSTR key ) const; |
CMapStringToPtr |
UINT HashKey( LPCTSTR key ) const; |
CMapWordToOb |
UINT HashKey( WORD key ) const; |
CMapWordToPtr |
UINT HashKey( WORD key ) const; |
Initializes the hash table.
void InitHashTable(
UINT hashSize,
BOOL bAllocNow = TRUE);
hashSize
Number of entries in the hash table.
bAllocNow
If TRUE
, allocates the hash table upon initialization; otherwise the table is allocated when needed.
For best performance, the hash table size should be a prime number. To minimize collisions, the size should be roughly 20 percent larger than the largest anticipated data set.
The following table shows other member functions that are similar to CMapStringToOb::InitHashTable
.
Class | Member Function |
---|---|
CMapPtrToPtr |
void InitHashTable( UINT hashSize , BOOL bAllocNow = TRUE ); |
CMapPtrToWord |
void InitHashTable( UINT hashSize , BOOL bAllocNow = TRUE ); |
CMapStringToString |
void InitHashTable( UINT hashSize , BOOL bAllocNow = TRUE ); |
CMapStringToPtr |
void InitHashTable( UINT hashSize , BOOL bAllocNow = TRUE ); |
CMapWordToOb |
void InitHashTable( UINT hashSize , BOOL bAllocNow = TRUE ); |
CMapWordToPtr |
void InitHashTable( UINT hashSize , BOOL bAllocNow = TRUE ); |
Determines whether the map is empty.
BOOL IsEmpty() const;
Nonzero if this map contains no elements; otherwise 0.
See the example for RemoveAll
.
The following table shows other member functions that are similar to CMapStringToOb:: IsEmpty
.
Class | Member Function |
---|---|
CMapPtrToPtr |
BOOL IsEmpty() const; |
CMapPtrToWord |
BOOL IsEmpty() const; |
CMapStringToPtr |
BOOL IsEmpty() const; |
CMapStringToString |
BOOL IsEmpty() const; |
CMapWordToOb |
BOOL IsEmpty() const; |
CMapWordToPtr |
BOOL IsEmpty() const; |
Returns a CObject
pointer based on a CString
value.
BOOL Lookup(
LPCTSTR key,
CObject*& rValue) const;
key
Specifies the string key that identifies the element to be looked up.
rValue
Specifies the returned value from the looked-up element.
Nonzero if the element was found; otherwise 0.
Lookup
uses a hashing algorithm to quickly find the map element with a key that matches exactly ( CString
value).
The following table shows other member functions that are similar to CMapStringToOb::LookUp
.
Class | Member Function |
---|---|
CMapPtrToPtr |
BOOL Lookup( void *key , void*& rValue ) const; |
CMapPtrToWord |
BOOL Lookup( void *key , WORD& rValue ) const; |
CMapStringToPtr |
BOOL Lookup( LPCTSTR key , void*& rValue ) const; |
CMapStringToString |
BOOL Lookup( LPCTSTR key , CString& rValue ) const; |
CMapWordToOb |
BOOL Lookup( WORD key , CObject*& rValue ) const; |
CMapWordToPtr |
BOOL Lookup( WORD key , void*& rValue ) const; |
See CObList::CObList
for a listing of the CAge
class used in all collection examples.
[!code-cppNVC_MFCCollections#68]
Returns a reference to the key associated with the specified key value.
BOOL LookupKey(
LPCTSTR key,
LPCTSTR& rKey) const;
key
Specifies the string key that identifies the element to be looked up.
rKey
The reference to the associated key.
Nonzero if the key was found; otherwise 0.
Using a reference to a key is unsafe if used after the associated element was removed from the map or after the map was destroyed.
The following table shows other member functions that are similar to CMapStringToOb:: LookupKey
.
Class | Member Function |
---|---|
CMapStringToPtr |
BOOL LookupKey( LPCTSTR key , LPCTSTR& rKey ) const; |
CMapStringToString |
BOOL LookupKey( LPCTSTR key , LPCTSTR& rKey ) const; |
A convenient substitute for the SetAt
member function.
CObject*& operator[ ](lpctstr key);
A reference to a pointer to a CObject
object; or NULL
if the map is empty or key
is out of range.
Thus it can be used only on the left side of an assignment statement (an l-value
). If there's no map element with the specified key, then a new element is created.
There's no "right side" (r-value
) equivalent to this operator because there's a possibility that a key may not be found in the map. Use the Lookup
member function for element retrieval.
The following table shows other member functions that are similar to CMapStringToOb::operator []
.
Class | Member Function |
---|---|
CMapPtrToPtr | void*& operator[](void *key ); |
CMapPtrToWord | WORD& operator[](void *key ); |
CMapStringToPtr | void*& operator[](lpctstr key ); |
CMapStringToString | CString& operator[](lpctstr key ); |
CMapWordToOb | CObject*& operator[](word key ); |
CMapWordToPtr | void*& operator[](word key ); |
See CObList::CObList
for a listing of the CAge
class used in all collection examples.
[!code-cppNVC_MFCCollections#72]
The results from this program are as follows:
Operator [] example: A CMapStringToOb with 2 elements
[Lisa] = a CAge at $4A02 11
[Bart] = a CAge at $497E 13
Removes all the elements from this map and destroys the CString
key objects.
void RemoveAll();
The CObject
objects referenced by each key aren't destroyed. The RemoveAll
function can cause memory leaks if you don't ensure that the referenced CObject
objects are destroyed.
The function works correctly if the map is already empty.
The following table shows other member functions that are similar to CMapStringToOb::RemoveAll
.
Class | Member Function |
---|---|
CMapPtrToPtr |
void RemoveAll(); |
CMapPtrToWord |
void RemoveAll(); |
CMapStringToPtr |
void RemoveAll(); |
CMapStringToString |
void RemoveAll(); |
CMapWordToOb |
void RemoveAll(); |
CMapWordToPtr |
void RemoveAll(); |
See CObList::CObList
for a listing of the CAge
class used in all collection examples.
[!code-cppNVC_MFCCollections#69]
Looks up the map entry corresponding to the supplied key; then, if the key is found, removes the entry.
BOOL RemoveKey(LPCTSTR key);
key
Specifies the string used for map lookup.
Nonzero if the entry was found and successfully removed; otherwise 0.
This can cause memory leaks if the CObject
object isn't deleted elsewhere.
The following table shows other member functions that are similar to CMapStringToOb::RemoveKey
.
Class | Member Function |
---|---|
CMapPtrToPtr |
BOOL RemoveKey( void *key ); |
CMapPtrToWord |
BOOL RemoveKey( void *key ); |
CMapStringToPtr |
BOOL RemoveKey( LPCTSTR key ); |
CMapStringToString |
BOOL RemoveKey( LPCTSTR key ); |
CMapWordToOb |
BOOL RemoveKey( WORD key ); |
CMapWordToPtr |
BOOL RemoveKey( WORD key ); |
See CObList::CObList
for a listing of the CAge
class used in all collection examples.
[!code-cppNVC_MFCCollections#70]
The results from this program are as follows:
RemoveKey example: A CMapStringToOb with 3 elements
[Marge] = a CAge at $49A0 35
[Homer] = a CAge at $495E 36
[Bart] = a CAge at $4634 13
The primary means to insert an element in a map.
void SetAt(
LPCTSTR key,
CObject* newValue);
key
Specifies the string that is the key of the new element.
newValue
Specifies the CObject
pointer that is the value of the new element.
First, the key is looked up. If the key is found, then the corresponding value is changed; otherwise a new key-value element is created.
The following table shows other member functions that are similar to CMapStringToOb::SetAt
.
Class | Member Function |
---|---|
CMapPtrToPtr |
void SetAt( void *key , void *newValue ); |
CMapPtrToWord |
void SetAt( void *key , WORD newValue ); |
CMapStringToPtr |
void SetAt( LPCTSTR key , void *newValue ); |
CMapStringToString |
void SetAt( LPCTSTR key , LPCTSTR newValue ); |
CMapWordToOb |
void SetAt( WORD key , CObject *newValue ); |
CMapWordToPtr |
void SetAt( WORD key , void *newValue ); |
See CObList::CObList
for a listing of the CAge
class used in all collection examples.
[!code-cppNVC_MFCCollections#71]
The results from this program are as follows:
before Lisa's birthday: A CMapStringToOb with 2 elements
[Lisa] = a CAge at $493C 11
[Bart] = a CAge at $4654 13
after Lisa's birthday: A CMapStringToOb with 2 elements
[Lisa] = a CAge at $49C0 12
[Bart] = a CAge at $4654 13
CObject
Class
Hierarchy Chart
CMapPtrToPtr
Class
CMapPtrToWord
Class
CMapStringToPtr
Class
CMapStringToString
Class
CMapWordToOb
Class
CMapWordToPtr
Class