본문 바로가기
C++

hash string

by SpeeDr00t 2016. 7. 12.
반응형

hash string

1.소스

#include <string>
#include <iostream>
#include <map>
#include <cstring>

using namespace std;

unsigned long getHashCode(const char* pString)
{
    unsigned long hash = 5381;
    int len = strlen(pString);
    for(int i = 0; i < len; ++i)
    {
        hash = ((hash << 5) + hash) + (unsigned long)pString[i]; // hash * 33 + ch
    }

return hash;
}

int main( int argc , char ** argv )
{

    map< unsigned long, unsigned long > container;
    container[ getHashCode("test") ] = 10;
    map< unsigned long , unsigned long >::iterator it = container.find( getHashCode("test") );
    cout << "key = " << (*it).first << endl;
    cout << "value = " << (*it).second << endl;

return 0;
}

2.결과

g++ -o hash_string hash_string.cpp

hacker@HACKER:~/cpp$ ./hash_string
key = 6385723493
value = 10


반응형

'C++' 카테고리의 다른 글

using for with a string  (0) 2016.07.29
point to string  (0) 2016.07.29
pointer  (0) 2016.07.28
데이터 비교해서 sink 맞추기  (0) 2016.07.12
i2string 숫자를 문자열로 변환  (0) 2016.07.12