複製鏈接
請複製以下鏈接發送給好友

GetNextAssoc

鎖定
GetNextAssoc,函數名稱,該函數在遍歷映射中所有元素時非常重要。
外文名
GetNextAssoc
性    質
函數名稱
類    別
函數
特    點
該函數在遍歷映射元素時非常重要

GetNextAssoc基本參數

CMap::GetNextAssoc
void GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) const
rNextPosition 指定一個以前GetNextAssoc或GetStartPosition調用返回的POSITION值的參考。 KEY 指定映射關鍵碼類型的模板參數。 rKey 指定被獲取元素返回的關鍵碼。 VALUE 指定映射值的類型的模板參數。 rValue 指定要獲取元素的返回值。
説明:
獲取rNextPosition位置的映射元素,然後將rNextPosition更新為映射中新的元素。該函數在遍歷映射中所有元素時非常重要。
如果獲取的元素為映射的最後一項,那麼rNextPosition的新值將設置為零。
CMapStringToOb::GetNextAssoc
void GetNextAssoc( POSITION& rNextPosition, CString& rKey, CObject*& rValue ) const;
參數:
rNextPosition
指定一個以前GetNextAssoc或GetStartPosition函數調用返回的POSITION值的參考。
rKey
指定被獲取元素(字符串)的返回關鍵碼。
rValue
指定要獲取元素(CObject指針)的返回值,請參閲説明以獲取該參數的更多信息。
説明:
值得注意的是位置次序與關鍵碼值次序不必相同。
對於參數rValue,要確保將對象類型設置為CObject*&形式,這是編譯器所需要的,如下面例子所示:
CMyObject* ob;
map.GetNextAssoc(pos, key, (CObject* &)ob);
這並不是根據模板建立的映射中GetNextAssoc的真實結果。

GetNextAssoc示例説明

請參閲CObList::CObList,瞭解所有收集示例中使用的CAge類。
//example for CMapStringToOb::GetNextAssoc and CMapStringToOb::GetStartPosition
CMapStringToOb map;
POSITION pos;
CString key;
CAge* pa;
map.SetAt( "Bart", new CAge( 13 ) );
map.SetAt( "Lisa", new CAge( 11 ) );
map.SetAt( "Homer", new CAge( 36 ) );
map.SetAt( "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 << " : " << pa << "\n";
#endif
該程序的運行結果如下:
Lisa : a CAge at $4724 11
Marge : a CAge at $47A8 35
Homer : a CAge at $4766 36
Bart : a CAge at $45D4 13