hash_map<string,int>的堆排序实现

我们知道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错误了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世纪殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值