CMapStringToPtr添加与释放
头文件.h
//--------------------------------------------------------------------
CMapStringToPtr m_prjFiles;
CStringList m_fileList;
CStringList m_nameList;
typedef struct
{
CString strPath;
CString strName;
}ST_LINE_INFO;
源文件.cpp
1、 插入元素
m_fileList.AddTail(_T("C:\\a"));
m_fileList.AddTail(_T("C:\\b"));
m_fileList.AddTail(_T("C:\\c"));
m_nameList.AddTail(_T("testA"));
m_nameList.AddTail(_T("testB"));
m_nameList.AddTail(_T("testC"));
for (POSITION pos = m_fileList.GetHeadPosition(), pos1 = m_nameList.GetHeadPosition(); pos&&pos1; m_fileList.GetNext(pos), m_nameList.GetNext(pos1))
{
ST_LINE_INFO* fi = new ST_LINE_INFO;
fi->strName = m_nameList.GetAt(pos1);
fi->strPath = m_fileList.GetAt(pos);
void* pVoid = NULL;
if (m_prjFiles.Lookup(fi->strName, pVoid))
{
delete fi;
fi = NULL;
}
else
{
m_prjFiles.SetAt(fi->strName, fi);
}
}
2、打印map元素信息按钮
void CMapStrToPtrDlg::OnBnClickedButton1()
{
POSITION pos = m_prjFiles.GetStartPosition();
CString info = _T("");
while (pos)
{
CString fileName;
ST_LINE_INFO* fi = NULL;
m_prjFiles.GetNextAssoc(pos, fileName, (void*&)fi);
if (fi)
{
OutputDebugString(fileName+_T("\n"));
OutputDebugString(fi->strPath+_T("\n"));
info += (fileName + _T("\n") + fi->strPath + _T("\n"));
fi = NULL;
}
}
MessageBox(info);
}
3、删除map元素按钮
void CMapStrToPtrDlg::OnBnClickedButton2()
{
for (POSITION pos = m_prjFiles.GetStartPosition(); pos;){
CString strKey;
ST_LINE_INFO* val = NULL;
m_prjFiles.GetNextAssoc(pos, strKey, (void*&)val);
if (val)
{
delete val;
val = NULL;
}
}
m_prjFiles.RemoveAll();
MessageBox(_T("删除成功!"));
}
|