2020-10-31每天一刷

这篇博客探讨了三种不同的哈希技术:字符哈希用于统计字符串中字符出现次数,哈希表用于排序整数,以及链地址法解决哈希冲突并构建哈希表。此外,还对比了哈希_map与STL_map的区别,强调了STL_map的有序性和迭代器遍历特性。
摘要由CSDN通过智能技术生成

字符哈希

用来统计一个字符串中,每个字符的个数。

#include "util.h"
using namespace std;

int main(){
    int char_map[128] = {0};
    string str = "abcdefgaaxxy";
    for (int i = 0; i < str.length(); ++i) {
        char_map[str[i]]++;
    }
    for (int i = 0; i < 128; ++i) {
        if (char_map[i] > 0){
            printf("[%c][%d] : %d\n",i,i,char_map[i]);
        }
    }
    return 0;
}

用hash表来排序整数

#include "util.h"
using namespace std;

int main(){
    int random [10] = {999,1,444,7,20,9,1,3,7,7};
    int hash_map[1000] ={0};
    for (int i = 0; i < 10; ++i) {
        hash_map[random[i]]++;
    }
    for (int i = 0; i < 1000; ++i) {
        for (int j = 0; j < hash_map[i]; ++j) {
            printf("%d\n",i);
        }
    }
    return 0;
}

链地址法解决哈希冲突,构造哈希表

#include "util.h"
using namespace std;
struct ListNode{
    int val;
    ListNode* next;
    ListNode(int x) : val(x),next(nullptr){}
};

int hash_func(int key, int table_len){
    return key % table_len;
}

void insert(ListNode* hash_table[], ListNode* node,int table_len){
    int hash_key = hash_func(node->val, table_len);
    node->next = hash_table[hash_key];
    hash_table[hash_key] = node;
}

bool search(ListNode* hash_table[],int value, int table_len){
    int hash_key = hash_func(value,table_len);
    ListNode* head = hash_table[hash_key];
    while (head){
        if (head->val == value){
            return true;
        }
        head = head->next;
    }
    return false;
}
int main(){
    const int TABLE_LEN = 11;
    ListNode* hash_table[TABLE_LEN] = {nullptr};
    vector<ListNode*> hash_node_vec;
    int test[8] = {1,1,4,9,20,30,150,500};
    for (int i = 0; i < 8; ++i) {
        hash_node_vec.push_back(new ListNode(test[i]));
    }
    for (int i = 0; i < hash_node_vec.size(); ++i) {
        insert(hash_table, hash_node_vec[i],TABLE_LEN);
    }
    printf("Hash table : \n");
    for (int i = 0; i < TABLE_LEN; ++i) {
        printf("[%d] : ",i);
        ListNode* head = hash_table[i];
        while (head){
            printf("->%d",head->val);
            head = head->next;
        }
        printf("\n");
    }
    printf("\n");
    printf("Test search: \n");
    for (int i = 0; i < 10; ++i) {
        if(search(hash_table,i,TABLE_LEN)){
            printf("%d is in the hash table.\n",i);
        }
        else{
            printf("%d is not in the hash table.\n",i);
        }
    }
    return 0;
}

hash_map和STL_map的区别

#include "util.h"
using namespace std;

int main(){
   map<string, int> hash_map;
   string str1 = "abc";
   string str2 = "aaa";
   string str3 = "xxxxx";
   hash_map[str1] = 1;
   hash_map[str2] = 2;
   hash_map[str3] = 100;
   if (hash_map.find(str1) != hash_map.end()){
       printf("%s is in hash_map, value is %d\n",
              str1.c_str(),hash_map[str1]);
   }
   for (auto it = hash_map.begin();it != hash_map.end();++it){
       printf("hash_map[%s] = %d\n",it->first.c_str(),it->second);
   }
    return 0;
}

STL 的底层是红黑树,所以他的hashmap 是自然有序的,因为是STL的容器,所以它可以使用迭代器来遍历容器中的每一个数字。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值