我们知道hash_map是利用多桶单链式的存储结构,现在我们根据int的数据进行hash_map排序,现在我们使用堆排序的方式,重新将hash_map中的key和value进行list<string>
和list<int>,然后根据int进行重新排序,同时将list<string>进行同步操作,以期获得根据value的string排序!!!!
// hash_map.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
#include<iostream>
#include<hash_map>
#include<string>
#include<list>
using namespace std;
struct hash_com{
static const size_t bucket_size = 4;
bool operator()(const string a, const string b) const
{
int m = strcmp(a.c_str(), b.c_str());
return m != 0;
}
size_t operator()(string b)
{
int i = 0;
int sum = 0;
while (b[i] != '\0')
{
sum += b[i++];
}
return sum % 5;
}
};
void str_position_i2_itr(list<string>* _list, int i, list<string>::iterator & m)
{
int m2 = i;
m = _list->begin();
while (m2 != 0)
{
m2--;
m++;
}
}
void int_position_i2_itr(list<int>* _list, int i, list<int>::iterator & m)
{
int m2 = i;
m = _list->begin();
while (m2 != 0)
{
m2--;
m++;
}
}
void HeapAdjust(list<string>* str_lis, list<int>*int_list, int i, int length)
{
int nChild = 0;
list<string>::iterator str_itr = str_lis->begin();
list<string>::iterator str_itr_1 = str_lis->begin();
list<int>::iterator int_itr = int_list->begin();
list<int>::iterator int_itr_1 = int_list->begin();
list<int>::iterator int_itr_2 = int_list->begin();
list<int>::iterator itr_max;
//将int_itr定位到int_list中的i所在位置
int_position_i2_itr(int_list, i, int_itr);
//得到子节点中较大的节点
for (; 2 * i + 1 < length; i = nChild)
{
//查找2*i和2*i+1之间比较大的值,确定位置即可
int_position_i2_itr(int_list, 2 * i, int_itr);
int_position_i2_itr(int_list, 2 * i + 1, int_itr_1);
if ((*int_itr) < (*int_itr_1))
{
//则int_itr_1为比较大的值
itr_max = int_itr_1;
nChild = 2 * i + 1;
}
else
{
//则int_itr为比较大的值
itr_max = int_itr;
nChild = 2 * i;
}
int_position_i2_itr(int_list, i, int_itr);
str_position_i2_itr(str_lis, i, str_itr);
str_position_i2_itr(str_lis, nChild, str_itr_1);
if (*itr_max > *int_itr)
{
//重置进行i和nChild的定位
int tmp = *itr_max;
*itr_max = *int_itr;
*int_itr = tmp;
string a = *str_itr;
*str_itr = *str_itr_1;
*str_itr_1 = a;
}
else
{
break;
}
}
}
void HeapSort(hash_map<string, int, hash_com>* hash_obj, list<string>* str_list, list<int>*int_list)
{
//开始进行堆排序
int n = hash_obj->size();
for (int i = n / 2 - 1; i >= 0; i--)
{
HeapAdjust(str_list, int_list, i, n);
}
for (int i = n - 1; i > 0; i--)
{
//使得首个元素最大
HeapAdjust(str_list, int_list, 0, i);
list<string>::iterator str_beg = str_list->begin();
list<string>::iterator str_itr = str_list->begin();
list<int>::iterator int_beg = int_list->begin();
list<int>::iterator int_itr = int_list->begin();
int_position_i2_itr(int_list, i, int_itr);
str_position_i2_itr(str_list, i, str_itr);
//进行 str_itr和str_beg int_itr和int_itr的替换
int a = *int_beg;
*int_beg = *int_itr;
*int_itr = a;
string mm = *str_beg;
*str_beg = *str_itr;
*str_itr = mm;
}
}
int main()
{
hash_map<string, int, hash_com> obj;
hash_map<string, int, hash_com>::iterator itr;
pair<string, int >mm("3", 3);
obj.insert(mm);
pair<string, int >mm1("5", 5);
obj.insert(mm1);
pair<string, int >mm2("9", 9);
obj.insert(mm2);
pair<string, int >mm4("15", 15);
obj.insert(mm4);
pair<string, int >mm3("4", 4);
obj.insert(mm3);
list<string> str_list;
list<string>::iterator str_itr = str_list.begin();;
list<int> int_list;
list<int>::iterator int_itr = int_list.begin();
for (itr = obj.begin(); itr != obj.end(); itr++)
{
str_list.push_back(itr->first);
int_list.push_back(itr->second);
}
//最后进行,堆排序
HeapSort(&obj, &str_list, &int_list);
for (itr = obj.begin(), str_itr = str_list.begin(), int_itr = int_list.begin(); str_itr != str_list.end(); itr++, str_itr++, int_itr++)
{
cout << *str_itr << ": " << *int_itr << endl;
}
}
好了,这下就完成了,
然而需要注意的是在hash_map的哈希函数和比较函数,在该结构体当中一定要加上
static const int bucket_size=8;
这样就不会出现error undefined bucket_size错误了