ハッシュコンテナ
4329 ワード
1.言語の手動構造hash関数
http://wenku.baidu.com/view/99972f69af1ffc4ffe47acb4.html
http://www.techpot.net/archives/37653
udating
bostライブラリハッシュコンテナunorded_set,unorded_map
4.
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.vc 6内蔵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.udating
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/ bostライブラリハッシュコンテナunorded_set,unorded_map
4.