您的位置:首页 > 其它

高效的遍历cmap中的元素方法

2017-07-02 09:02 169 查看
我看师兄的代码 是直接从0 一直循环到m_nHashTableSize,cmap里面实现代码removeall也是这样循环的,但是如果m_nHashTableSize为几十万的数值,而里面存储的元素只有几十个的话,感觉效率不高。有没有其他的高效方法遍历cmap中的所有元素?
本文地址 :CodeGo.net/6122382/ 
------------------------------------------------------------------------------------------------------------------------- 
1.MFC的CMap ?
GetStartPosition + GetNextAssoc 遍历
MSDN上的例子
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);
}
// Print the element values.
pos = myMap.GetStartPosition();
while (pos != NULL)
{
myMap.GetNextAssoc(pos, nKey, pt);
_tprintf_s(_T("Current key value at %d: %d,%d\n"),
nKey, pt.x, pt.y);
}


本文标题 :有没有高效的遍历cmap中的元素方法?
本文地址 :CodeGo.net/6122382/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: