Example 1
|
CMap<int,int,CPoint,CPoint> myMap;
// Add 10 elements to the map.
for (int i=0;i
< 10;i++)
myMap.SetAt( i, CPoint(i, i)
);
// Remove the elements with even key values.
POSITION pos = myMap.GetStartPosition();
int nKey;
CPoint pt;
while (pos != NULL)
{
myMap.GetNextAssoc( pos, nKey,
pt );
if ((nKey%2) == 0) myMap.RemoveKey(
nKey );
} #ifdef
_DEBUG afxDump.SetDepth(
1 ); afxDump
<< "myMap: " << &myMap << "\n"; #endif |
|
|
Example 3
typedef CMap<DWORD,DWORD,CMyStruct*,CMyStruct*&> CMapDWordToMyStruct; CMapDWordToMyStruct m_mapDWordToMyStruct; POSITION pos = m_mapDWordToMyStruct.GetStartPosition();while (pos != NULL){DWORD dwKey;CMyStruct* pMyStruct; m_mapDWordToMyStruct.GetNextAssoc(pos, dwKey, pMyStruct); delete pMyStruct;}// Remove all the fields.m_mapDWordToMyStruct.RemoveAll(); CMyStruct* pTemp = GetDocument()->m_mapDWordToMyStruct[m_dwKey]; CMyStruct* pMyStruct; m_mapDWordToMyStruct.Lookup(m_dwKey, pMyStruct); CMyStruct* pMyStruct = new CMyStruct(); m_mapDWordToMyStruct[m_dwKey] = pMyStruct; m_mapDWordToMyStruct.Serialize(ar); |
Example 4
typedef CMap<CString,LPCSTR,CRect,CRect&> CMapCStringToCRect; CMapCStringToCRect mapStrToRect; BOOL CScaleDlg::OnInitDialog() {CDialog::OnInitDialog(); CFile mFile; if(mFile.Open(theApp.GetCurDir()+"dlgStatus.sav",CFile::modeRead)&& mFile.GetLength()>0 ){CArchive ar(&mFile,CArchive::load); mapStrToRect.Serialize(ar); ar.Close(); mFile.Close(); CString strWindow; GetWindowText(strWindow); CRect rectDlg; BOOL bSuc=mapStrToRect.Lookup(strWindow,rectDlg);if(bSuc)MoveWindow(rectDlg); }return TRUE;}void CScaleDlg::OnDestroy() {CDialog::OnDestroy(); CRect rectDlg; GetWindowRect(&rectDlg); CString strWindow; GetWindowText(strWindow); mapStrToRect.SetAt(strWindow,rectDlg); CFile mFile; mFile.Open(theApp.GetCurDir()+"dlgStatus.sav",CFile::modeCreate|CFile::modeWrite); CArchive ar(&mFile,CArchive::store); mapStrToRect.Serialize(ar); ar.Close(); mFile.Close(); } |
Example 5
|
class CStringObject
: public CObject { public: CString m_String; CStringObject() {}; CStringObject(LPCSTR String) : m_String(String)
{}; // Copy
constructor required by CMap. CStringObject(const CStringObject& n) { m_String
= n.m_String; } // Operator= is
required by CMap. CStringObject&
operator=(const CStringObject& n) { m_String
= n.m_String; return *this; } BOOL AFXAPI
operator==(const CStringObject& n) const { return m_String
== n.m_String; } }; template<>inline
UINT AFXAPI HashKey(CStringObject&
key) { return HashKey((LPCTSTR)key.m_String); } typedef CMap<CStringObject, CStringObject&, CStringObject,
CStringObject&> CStringMap; The following is a sample use of the CStringMap class. CStringObject KeyString, DataString,
One("One"), Two("Two"); CStringMap StringMap; POSITION Position; StringMap[One] = CStringObject("First"); StringMap[Two] = CStringObject("Second"); Position = StringMap.GetStartPosition(); while
(Position!=NULL) { StringMap.GetNextAssoc(Position, KeyString, DataString); cout << (LPCSTR)KeyString.m_String
<< ' ' << (LPCSTR)DataString.m_String
<< '\n'; } cout << (LPCSTR)StringMap[One].m_String << '\n'; StringMap.RemoveAll(); |