关于hash_map数据存储问题

whm2300 2013-03-26 08:23:50
//测试函数
void test_hash()
{
std::hash_map<TestKey, Person, TestKeyHashFun, TestKeyEqual> testMap(100);
TestKey tmpKey("chenqingming");
Person tmpValue("ming");
testMap[tmpKey] = tmpValue;
}
//哈希函数和比较函数
struct TestKeyHashFun
{
unsigned int operator() (const TestKey& value)const
{
const unsigned int seed = 131;
unsigned int hash = 0;

std::string::const_iterator pos = value.m_str.begin();
while (value.m_str.end() != pos)
{
hash = hash*seed + *pos;
++pos;
}
return hash;
}
};

struct TestKeyEqual
{
bool operator() (const TestKey& lhs, const TestKey& rhs)const
{
return (lhs.m_str == rhs.m_str) && (lhs.m_nValue == rhs.m_nValue);
}
};

环境:32位win7 vs2010 STLport
运行函数,结果如下:
testkey_constructer
person_constructer
testkey_copyconstructer
person_copyconstruct
testkey_copyconstructer
person_copyconstruct
~person
~testkey
~person
~person
~testkey
~person
~testkey

47 ms
请按任意键继续. . .

疑问:
① 为什么构造函数(含拷贝构造)一共才六次,而却析构了七次?多析构一次怎么解释?
② hasn_map中值存储是个什么过程?
...全文
50 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧