关于hash_map数据存储问题
//测试函数
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中值存储是个什么过程?
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中值存储是个什么过程?
...全文
相关推荐
C++ std::hash详解[可运行源码]
std::hash是C++标准库中的一个模板类,用于计算对象的哈希值,常用于哈希表如std::unordered_map和std::unordered_set中。文章详细介绍了std::hash的用法,包括对基本类型(如int、float、std::string等)的特化版本,以及如何通过实例化std::hash模板并调用其operator()来计算哈希值。此外,还提供了多个示例代码,如哈希整数和字符串,并解释了哈希函数的作用及其在不同标准库实现中的行为差异。对于自定义类型,文章展示了如何通过特化std::hash模板来定义自己的哈希函数,并提供了具体实现示例。最后,总结了std::hash的实用性和灵活性,强调了其在快速查找和存储数据中的重要性。
亿万级数据处理的高效解决方案.docx
亿万级数据处理的高效解决方案.docx
Python字典的核心底层原理讲解
今天小编就为大家分享一篇关于Python字典的核心底层原理讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
leetcode-Hashing-2:Hashing-2
leetcode Hashing-2 问题1 () 问题2 () 问题3 ()
Ruby Mind Map
ruby mind v0.8 博文链接:https://sharkmao.iteye.com/blog/46951


工具平台和程序库
