使用简单原始的办法排序CMap中的路径

编者:李国帅

qq:9611153 微信lgs9611153

时间:2012-9-10 9:26:02

背景原因:

        

解决一个简单的问题,把多个路径放入容器,先查询或使用优先级高的路径,然后依次按照优先级依次访问。

         CMap没有排序,有时需要设置优先级。设定CMap<优先级,路径>;优先级数值越大越靠后。

 

所需资源:

VC,MFC

 

解决方案:

 

 

CString GetPriorityPath(CMap<LONG,LONG,CString,CString>& mapRecordPathNew)

{

         CString strRecordPathMin = _T("");

         //CString strValue;

 

         LONG nPriority;

         CString strRecordPath;

         LONG nPriorityMin = (1<<16);

         POSITION pos = mapRecordPathNew.GetStartPosition();

         while(pos)

         {

                   mapRecordPathNew.GetNextAssoc(pos, nPriority,strRecordPath);

                   //strValue.Format(_T("%d:%s"),nPriority, strRecordPath);

                   //OutputDebugString(strValue);

                   if (nPriority < nPriorityMin)

                   {

                            nPriorityMin = nPriority;

                            strRecordPathMin = strRecordPath;

                   }

         }

         //mapRecordPathNew.Lookup( nPriorityMin, strRecordPath )

         //strValue.Format(_T("%d:%s \n"),nPriorityMin, strRecordPathMin);

         //OutputDebugString(strValue);

 

         if (nPriorityMin != (1<<16))

         {

                   mapRecordPathNew.RemoveKey(nPriorityMin);

         }

         return strRecordPathMin;

}

void CopyMap(CMap<LONG,LONG,CString,CString>& mapRecordPathNew, CMap<LONG,LONG,CString,CString>& mapRecordPath)

{

         LONG nPriority;

         CString strRecordPath;

         POSITION pos = mapRecordPath.GetStartPosition();

         while(pos)

         {

                   mapRecordPath.GetNextAssoc(pos, nPriority,strRecordPath);

                   mapRecordPathNew.SetAt(nPriority,strRecordPath);

         }

         return ;

}

 

void CSimpleDlgDlg::OnBnClickedButton1()

{

 

         CMap<LONG,LONG,CString,CString> m_mapRecordPath;

 

         m_mapRecordPath.InitHashTable( 16 );

 

         m_mapRecordPath[11] = _T("c:\\");

         m_mapRecordPath[3] = _T("d:\\");

         m_mapRecordPath[5] = _T("e:\\");

 

         CMap<LONG,LONG,CString,CString> mapRecordPathNew;

         CopyMap(mapRecordPathNew,m_mapRecordPath);

 

         CString strValue = GetPriorityPath(mapRecordPathNew);

         OutputDebugString(strValue);

         OutputDebugString( _T("\n"));

         strValue = GetPriorityPath(mapRecordPathNew);

         OutputDebugString(strValue);

         OutputDebugString( _T("\n"));

         strValue = GetPriorityPath(mapRecordPathNew);

         OutputDebugString(strValue);

         OutputDebugString( _T("\n"));

         strValue = GetPriorityPath(mapRecordPathNew);

         OutputDebugString(strValue);

}

 

 

 

输出:

d:\

e:\

c:\

 

 

猜你喜欢

转载自blog.csdn.net/lgs790709/article/details/84823368