CMapStringToString Class

Supports maps of CString objects keyed by CString objects.

Syntax

class CMapStringToString : public CObject

Members

The member functions of CMapStringToString are similar to the member functions of class CMapStringToOb. Because of this similarity, you can use the CMapStringToOb reference documentation for member function specifics. Wherever you see a CObject pointer as a return value or "output" function parameter, substitute a pointer to char. Wherever you see a CObject pointer as an "input" function parameter, substitute a pointer to char.

BOOL CMapStringToString::Lookup(LPCTSTR<key>, CString&<rValue>) const;

for example, translates to

BOOL CMapStringToOb::Lookup(const char*<key>, CObject*&<rValue>) const;

Public Structures

Name Description
CMapStringToString::CPair A nested structure containing a key value and the value of the associated string object.

Public Constructors

Name Description
CMapStringToString::CMapStringToString Constructor.

Public Methods

Name Description
CMapStringToString::GetCount Returns the number of elements in this map.
CMapStringToString::GetHashTableSize Determines the current number of elements in the hash table.
CMapStringToString::GetNextAssoc Gets the next element for iterating.
CMapStringToString::GetSize Returns the number of elements in this map.
CMapStringToString::GetStartPosition Returns the position of the first element.
CMapStringToString::HashKey Calculates the hash value of a specified key.
CMapStringToString::InitHashTable Initializes the hash table.
CMapStringToString::IsEmpty Tests for the empty-map condition (no elements).
CMapStringToString::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.
CMapStringToString::LookupKey Returns a reference to the key associated with the specified key value.
CMapStringToString::PGetFirstAssoc Gets a pointer to the first CString in the map.
CMapStringToString::PGetNextAssoc Gets a pointer to the next CString for iterating.
CMapStringToString::PLookup Returns a pointer to a CString whose value matches the specified value.
CMapStringToString::RemoveAll Removes all the elements from this map.
CMapStringToString::RemoveKey Removes an element specified by a key.
CMapStringToString::SetAt Inserts an element into the map; replaces an existing element if a matching key is found.

Public Operators

Name Description
CMapStringToString::operator [ ] Inserts an element into the map — operator substitution for SetAt.

Remarks

CMapStringToString 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 dump of individual CString- CString elements, you must set the depth of the dump context to 1 or greater.

When a CMapStringToString object is deleted, or when its elements are removed, the CString objects are removed as appropriate.

For more information on CMapStringToString, see the article Collections.

Inheritance Hierarchy

CObject

CMapStringToString

Requirements

Header: afxcoll.h

CMapStringToString::CPair

Contains a key value and the value of the associated string object.

Remarks

This is a nested structure within class CMapStringToString.

The structure is composed of two fields:

  • key The actual value of the key type.

  • value The value of the associated object.

It is used to store the return values from CMapStringToString::PLookup, CMapStringToString::PGetFirstAssoc, and CMapStringToString::PGetNextAssoc.

Example

For an example of usage, see the example for CMapStringToString::PLookup.

CMapStringToString::PGetFirstAssoc

Returns the first entry of the map object.

const CPair* PGetFirstAssoc() const;

CPair* PGetFirstAssoc();

Return Value

A pointer to the first entry in the map; see CMapStringToString::CPair. If the map is empty, the value is NULL.

Remarks

Call this function to return a pointer the first element in the map object.

Example

CMapStringToString myMap;
CString myStr[4] = {_T("One"), _T("Two"), _T("Three"), _T("Four")};
CMapStringToString::CPair *pCurVal;

myMap.InitHashTable(257);

// Add 4 elements to the map.
myMap.SetAt(myStr[0], _T("Odd"));
myMap.SetAt(myStr[1], _T("Even"));
myMap.SetAt(myStr[2], _T("Odd"));
myMap.SetAt(myStr[3], _T("Even"));

pCurVal = myMap.PGetFirstAssoc();
while (pCurVal != NULL)
{
   _tprintf_s(_T("Current key value at %s: %s\n"),
              pCurVal->key, pCurVal->value);
   pCurVal = myMap.PGetNextAssoc(pCurVal);
}

CMapStringToString::PGetNextAssoc

Retrieves the map element pointed to by pAssocRec.

const CPair *PGetNextAssoc(const CPair* pAssoc) const;

CPair *PGetNextAssoc(const CPair* pAssoc);

Parameters

pAssoc
Points to a map entry returned by a previous PGetNextAssoc or PGetFirstAssoc call.

Return Value

A pointer to the next entry in the map; see CMapStringToString::CPair. If the element is the last in the map, the value is NULL.

Remarks

Call this method to iterate through all the elements in the map. Retrieve the first element with a call to PGetFirstAssoc and then iterate through the map with successive calls to PGetNextAssoc.

Example

See the example for CMapStringToString::PGetFirstAssoc.

CMapStringToString::PLookup

Looks up the value mapped to a given key.

const CPair* PLookup(LPCTSTR key) const;

CPair* PLookup(LPCTSTR key);

Parameters

key
A pointer to the key for the element to be searched for.

Return Value

A pointer to the specified key.

Remarks

Call this method to search for a map element with a key that exactly matches the given key.

Example

CMapStringToString myMap;
CString myStr[4] = {_T("One"), _T("Two"), _T("Three"), _T("Four")};

myMap.InitHashTable(257);

// Add 4 elements to the map.
myMap.SetAt(myStr[0], _T("Odd"));
myMap.SetAt(myStr[1], _T("Even"));
myMap.SetAt(myStr[2], _T("Odd"));
myMap.SetAt(myStr[3], _T("Even"));

// Print the element values with odd key values.
CMapStringToString::CPair *pCurVal;

for (int i = 0; i < 4; i += 2)
{
   pCurVal = myMap.PLookup(myStr[i]);
   _tprintf_s(_T("Current key value at %s: %s\n"),
              pCurVal->key, pCurVal->value);
}

See also

MFC Sample COLLECT
CObject Class
Hierarchy Chart