我是新来的散列图在c + +。我必须将表转换为hashmap。hash_map第一次查找时大小为零时崩溃
这是怎么了我已经宣布,在我的节目
我使用Microsoft Visual Studio使用的hash_map。
#include <hash_map>
using namespace stdext;
typedef hash_multimap <const char*, long > HEAPTABLE;
typedef HEAPTABLE::iterator HEAP_ITER;
class CTest
{
public:
void setSwitchID(long i);
long getSwitchID();
void isUpgrading(bool bTest);
private:
HEAPTABLE m_hashMap;
};
void CTest::setSwitchID(long dwID)
{
HEAP_ITER hIter = m_hashMap.find("SwitchId");
if (hIter != m_hashMap.end())
{
hIter->second = dwID;
}
else
{
m_hashMap.insert(make_pair("SwitchId", dwID));
}
}
long CTest::getSwitchID()
{
HEAP_ITER hIter = m_hashMap.find("SwitchId");
if (hIter != m_hashMap.end())
{
return hIter->second;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
CTest* test = new CTest;
if (test)
{
test->setSwitchID((DWORD)i);
test->isUpgrading(false);
}
delete test;
return 0;
}
此代码工作正常,当我运行它作为一个单独的程序,但是当我尝试运行它作为我的项目的应用程序崩溃的一部分。即使映射中没有条目,set函数中的hIter也会返回错误的指针。这是因为存在腐败吗?能否请你帮忙?
如果是堆腐败,我该如何避免这种情况?无论如何,我可以说创建一个这个大小的hash_map?
你是否需要使用'hash_map'(或任何非标准容器)?你不能只使用'std :: map'吗? – birryree 2010-12-11 04:35:18
请注意,hash_map仅适用于Microsoft Visual Studio,您可能会考虑学习未来标准的unordered_map。 – unsym 2010-12-11 04:36:56