关于hash_map数据存储问题

whm2300 whm2300 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
//测试函数
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中值存储是个什么过程?
...全文
评论
收藏 已收藏
分享
返回顶部
whm2300
工具平台和程序库 24,849 成员 27,328 内容