共用方式為


CMapStringToOb::GetNextAssoc

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;

Parameters

  • 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.

Remarks

This function is most useful for iterating through all the elements in the map. Note that the position sequence is not 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:

CObject* ob;
map.GetNextAssoc(pos, key, (CObject*&)ob);      

This is not 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;

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

CMapStringToOb map;
POSITION pos;
CString key;
CAge* pa;

map.SetAt(_T("Bart"), new CAge(13));
map.SetAt(_T("Lisa"), new CAge(11));
map.SetAt(_T("Homer"), new CAge(36));
map.SetAt(_T("Marge"), new CAge(35));
// Iterate through the entire map, dumping both name and age. 
for (pos = map.GetStartPosition(); pos != NULL;)
{
   map.GetNextAssoc(pos, key, (CObject*&)pa);
   #ifdef _DEBUG
         afxDump << key << _T(" : ") << pa << _T("\n");
   #endif
}

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

Requirements

Header: afxcoll.h

See Also

Reference

CMapStringToOb Class

Hierarchy Chart

CMapStringToOb::GetStartPosition