如果要在hash_map中把自已定义的类作为key的话要怎么做?这种情况下需要定义计算自定义的hash函数和比较自定义类的比较函数
#include
#include
#include
using namespace std;
class A
{
public:
A(int a, int b) : m_a(a), m_b(b) {}
A(const A& a)
{
printf("Copy construction\n");
m_a = a.m_a;
m_b = a.m_b;
}
~A() {}
public:
int HashVal() const
{
return m_a * m_b;
}
int GetV() const
{
return m_a + m_b;
}
private:
int m_a;
int m_b;
};
struct A_Hash_Compare : public hash_compare
{
size_t operator()(const A& a)
{
printf("count hash val\n");
return a.HashVal();
}
bool operator()(const A& a, const A& b)
{
printf("compare\n");
return a.GetV() < b.GetV();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
hash_map A_hm;
A a = A(1, 2);
A b = A(1, 3);
A_hm[a] = 123;
printf("\n");
A_hm.insert(pair(b, 1));
printf("\n");
printf("%d\n", A_hm[a]);
getchar();
return 0;
}
count hash val
Copy construction
Copy construction
count hash val
Copy construction
Copy construction
count hash val
count hash val
compare
compare
123
插入和初始化
#include
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
hash_map i_hm;
//首次插入元素
pair::iterator, bool> ret = i_hm.insert(pair(1, 1));
printf("ret.second: %d, ret.first->first: %d, ret.first->second: %d\n", ret.second, ret.first->first, ret.first->second);
//插入同样的元素
ret = i_hm.insert(pair(1, 1));
printf("ret.second: %d, ret.first->first: %d, ret.first->second: %d\n", ret.second, ret.first->first, ret.first->second);
//首次插入元素
hash_map:: iterator iter = i_hm.insert(i_hm.begin(), pair(2, 2));
printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
//插入同样的元素
iter = i_hm.insert(i_hm.begin(), pair(2, 2));
printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
//插入initalizer_list,返回值为void,需要c++ 11支持
i_hm.insert({ pair(3, 3), pair(4, 6), pair(5, 10) });
//输出元素
hash_map::iterator i_iter = i_hm.begin();
for (; i_iter != i_hm.end(); ++i_iter)
printf("i_hm[%d] = %d\n", i_iter->first, i_iter->second);
printf("\n");
hash_map j_hm;
//返回值为void
j_hm.insert(i_hm.begin(), i_hm.end());
//输出元素
hash_map::iterator j_iter = j_hm.begin();
for (; j_iter != j_hm.end(); ++j_iter)
printf("j_hm[%d] = %d\n", j_iter->first, j_iter->second);
printf("\n");
//初始化
hash_map k_hm(j_hm);
//输出元素
hash_map::iterator k_iter = k_hm.begin();
for (; k_iter != k_hm.end(); ++k_iter)
printf("k_hm[%d] = %d\n", k_iter->first, k_iter->second);
printf("\n");
hash_map m_hm(j_hm.begin(), j_hm.end());
//输出元素
hash_map::iterator m_iter = m_hm.begin();
for (; m_iter != m_hm.end(); ++m_iter)
printf("m_hm[%d] = %d\n", m_iter->first, m_iter->second);
printf("\n");
hash_map n_hm = j_hm;
//输出元素
hash_map::iterator n_iter = n_hm.begin();
for (; n_iter != n_hm.end(); ++n_iter)
printf("n_hm[%d] = %d\n", n_iter->first, n_iter->second);
printf("\n");
ret.second: 1, ret.first->first: 1, ret.first->second: 1
ret.second: 0, ret.first->first: 1, ret.first->second: 1
iter->first: 2, iter->second: 2
iter->first: 2, iter->second: 2
i_hm[1] = 1
i_hm[2] = 2
i_hm[3] = 3
i_hm[4] = 6
i_hm[5] = 10
j_hm[1] = 1
j_hm[2] = 2
j_hm[3] = 3
j_hm[4] = 6
j_hm[5] = 10
k_hm[1] = 1
k_hm[2] = 2
k_hm[3] = 3
k_hm[4] = 6
k_hm[5] = 10
m_hm[1] = 1
m_hm[2] = 2
m_hm[3] = 3
m_hm[4] = 6
m_hm[5] = 10
n_hm[1] = 1
n_hm[2] = 2
n_hm[3] = 3
n_hm[4] = 6
n_hm[5] = 10
查找、删除、交换、清空
#include
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
hash_map i_hm;
//首次插入元素
pair::iterator, bool> ret = i_hm.insert(pair(1, 1));
printf("ret.second: %d, ret.first->first: %d, ret.first->second: %d\n", ret.second, ret.first->first, ret.first->second);
//插入同样的元素
ret = i_hm.insert(pair(1, 1));
printf("ret.second: %d, ret.first->first: %d, ret.first->second: %d\n", ret.second, ret.first->first, ret.first->second);
//首次插入元素
hash_map:: iterator iter = i_hm.insert(i_hm.begin(), pair(2, 2));
printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
//插入同样的元素
iter = i_hm.insert(i_hm.begin(), pair(2, 2));
printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
//插入initalizer_list,返回值为void,需要c++ 11支持
i_hm.insert({ pair(3, 3), pair(4, 6), pair(5, 10) });
printf("\n");
//查找元素
iter = i_hm.find(1);
printf("查找key:1\n");
if (iter != i_hm.end())
printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
else
printf("iter == end\n");
printf("查找key:100\n");
iter = i_hm.find(100);
if (iter != i_hm.end())
printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
else
printf("iter == end\n");
printf("\n");
//返回删除的元素个数
size_t num = i_hm.erase(1);
printf("num: %d\n", num);
num = i_hm.erase(1);
printf("num: %d\n", num);
//返回的迭代器指向下一个元素
iter = i_hm.erase(i_hm.begin());
if (iter != i_hm.end())
printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
//返回的迭代器指向下一个元素
iter = i_hm.erase(i_hm.begin(), i_hm.end());
if (iter != i_hm.end())
printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
else
printf("iter == end\n");
printf("\n");
//swap函数
i_hm.insert({ pair(3, 3), pair(4, 6), pair(5, 10) });
hash_map j_hm({ pair(11, 3), pair(12, 6)});
j_hm.swap(i_hm);
iter = i_hm.begin();
for (; iter != i_hm.end(); ++iter)
printf("i_hm[%d] = %d\n", iter->first, iter->second);
printf("\n");
iter = j_hm.begin();
for (; iter != j_hm.end(); ++iter)
printf("j_hm[%d] = %d\n", iter->first, iter->second);
printf("\n");
//清空
i_hm.clear();
j_hm.clear();
iter = i_hm.begin();
for (; iter != i_hm.end(); ++iter)
printf("i_hm[%d] = %d\n", iter->first, iter->second);
printf("\n");
iter = j_hm.begin();
for (; iter != j_hm.end(); ++iter)
printf("j_hm[%d] = %d\n", iter->first, iter->second);
printf("\n");
getchar();
return 0;
}
ret.second: 1, ret.first->first: 1, ret.first->second: 1
ret.second: 0, ret.first->first: 1, ret.first->second: 1
iter->first: 2, iter->second: 2
iter->first: 2, iter->second: 2
查找key:1
iter->first: 1, iter->second: 1
查找key:100
iter == end
num: 1
num: 0
iter->first: 3, iter->second: 3
iter == end
i_hm[11] = 3
i_hm[12] = 6
j_hm[3] = 3
j_hm[4] = 6
j_hm[5] = 10