散列容器

1.c语言手动构造hash函数
http://wenku.baidu.com/view/99972f69af1ffc4ffe47acb4.html
// hashfunc.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
using namespace std;
char *key[32]={"auto","break","case","char","const",
"continue","default","do","double",
"else","enum","extern",
"float","for","goto","if","int","long",
"register", "return", "short", 
"signed","sizeof","static", "struct","switch",
"typedef","union","unsigned",
"void","volatile","while"};

//生成的hash值在0-252之间
int  hash(char *string)
{
	int i=1;
	int ix = 0;
	for(;*string;string++)
	{
		ix =ix + i*(*string);
		i++;
	}
	ix = ix%253;
	return ix;
};
int findkey(char * str)
{
	int i;
	int index[32];
	char *HASH[253];
	for(i=0;i<=252;i++)
	{
		HASH[i]="";
	}
	for(i=0;i<32;i++)
	{
	//	index[i] = hash(key[i]);//依次取得每个key的hash值即数组的下标,储存在index数组中
	//	HASH[index[i]] = key[i];//将key的存入数组中对应的元素
		HASH[hash(key[i])] = key[i];//将key的存入数组中对应的元素
	}
	if(strcmp(str,HASH[hash(str)])!=0)
		return 0;
	else 
		return 1;
};

int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"关键字的哈希值为:"<<endl;
	int i;
	for(i=0;i<32;i++)
	{
		cout<<hash(key[i])<<"\t";
		if(i!=0&&i%5==0)
			cout<<endl;
	}
	cout<<endl;

	char string[20]={0};
	int choice;
	do
	{
		cout<<"请输入你的选择:1,查找关键字;,退出查找:"<<endl;
		cin>>choice;
		if(choice == 1)
		{
			cout<<"请输入你要查找的关键字:"<<endl;
			cin>>string;

			if(findkey(string))
				cout<<"你所输入的字符串是关键字"<<endl;
			else
				cout<<"你所输入的字符串不是关键字"<<endl;

		}
	}while(choice!=2);
	return 0;
}


2.vc6 调用内置hash函数
http://www.techpot.net/archives/37653
// 计算Hash,成功返回0,失败返回GetLastError()
//  CONST BYTE *pbData, // 输入数据www.2cto.com
//  DWORD dwDataLen,     // 输入数据字节长度
//  ALG_ID algId       // Hash 算法:CALG_MD5,CALG_SHA
//  LPTSTR pszHash,  // 输出16进制Hash字符串,MD5长度为32+1, SHA长度为40+1

DWORD CHash1Dlg::GetHash(BYTE *pbData, DWORD dwDataLen, ALG_ID algId, LPTSTR pszHash)
{
	DWORD dwReturn = 0;
	HCRYPTPROV hProv;
	if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
		return (dwReturn = GetLastError());
	
	HCRYPTHASH hHash;
	//Alg Id:CALG_MD5,CALG_SHA
	if(!CryptCreateHash(hProv, algId, 0, 0, &hHash))
	{
		dwReturn = GetLastError();
		CryptReleaseContext(hProv, 0);
		return dwReturn;
	}
	
	if(!CryptHashData(hHash, pbData, dwDataLen, 0))
	{
		dwReturn = GetLastError();
		CryptDestroyHash(hHash);
		CryptReleaseContext(hProv, 0);
		return dwReturn;
	}
	
	DWORD dwSize;
	DWORD dwLen = sizeof(dwSize);
	CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)(&dwSize), &dwLen, 0);
	
	BYTE* pHash = new BYTE[dwSize];
	dwLen = dwSize;
	CryptGetHashParam(hHash, HP_HASHVAL, pHash, &dwLen, 0);
	
	lstrcpy(pszHash, _T(""));
	TCHAR szTemp[3];
	for (DWORD i = 0; i < dwLen; ++i)
	{
		//wsprintf(szTemp, _T(“%X%X”), pHash[i] >> 4, pHash[i] & 0xf);
		wsprintf(szTemp, "%02X", pHash[i]);
		lstrcat(pszHash, szTemp);
	}
	delete [] pHash;
	
	CryptDestroyHash(hHash);
	CryptReleaseContext(hProv, 0);
	return dwReturn;
	
}


void CHash1Dlg::OnOK() 
{

	TCHAR szStr[20] = {0};
	TCHAR szHash[41] = {0};
	
	strcpy(szStr,"Hello,Hash!你好,哈希!");
	GetHash((BYTE*)szStr, strlen(szStr), CALG_MD5,szHash);
	//MessageBox(szHash,MB_OK);
	TRACE("szHash=%s",szHash);
}
在头文件中加入下列代码
#define _WIN32_WINNT 0x0400
#include <tchar.h>
#include <wincrypt.h>


3.
updating
struct MyStruct
{

	CString name;
	int age;
};

CList<MyStruct ,MyStruct&> my_list_global;

MyStruct mystruct;
for (int i=1;i<10;i++)
{
	mystruct.age=i;
	CString str_name=itoa(i);
	mystruct.name=str_name+"HELLP";
	my_list_global.AddTail(mystruct);
}
http://geeklu.com/2010/07/hash-table/

boost库散列容器 unorderd_set,unorderd_map


4.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值