编程语言(C++/Python/C#/javascript)中的数据结构——哈希映射

哈希表简介

  • 哈希表是一种使用哈希函数组织数据,以支持快速插入和搜索的数据结构。
  • 有两种不同类型的哈希表哈希集合(理解为set)和哈希映射(理解为dictionary)。
    • 哈希集合是集合数据结构的实现之一,用于存储非重复值。
    • 哈希映射是映射数据结构的实现之一,用于存储(key, value)键值对。
  • 在标准模板库的帮助下,哈希表是易于使用的。大多数常见语言(如Java,C ++ 和 Python)都支持哈希集合和哈希映射。
  • 通过选择合适的哈希函数,哈希表可以在插入和搜索方面实现出色的性能。

哈希映射

简介

  • 哈希映射是映射的一种实现,它能够存储 (key,value) 键值对。
  • 由于能够存储更多信息,哈希映射可以帮助我们解决更复杂的问题。 例如,我们可以使用哈希映射按键聚合所有信息,并在平均为常量的时间内查找与特定键相关的信息。

C++

#include <unordered_map>  // 0. include the library

int main() {
    // 1. initialize a hash map
    unordered_map<int, int> hashmap;
    // 2. insert a new (key, value) pair
    hashmap.insert(make_pair(0, 0));
    hashmap.insert(make_pair(2, 3));
    // 3. insert a new (key, value) pair or update the value of existed key
    hashmap[1] = 1;
    hashmap[1] = 2;
    // 4. get the value of a specific key
    cout << "The value of key 1 is: " << hashmap[1] << endl;
    // 5. delete a key
    hashmap.erase(2);
    // 6. check if a key is in the hash map
    if (hashmap.count(2) <= 0) {
        cout << "Key 2 is not in the hash map." << endl;
    }
    // 7. get the size of the hash map
    cout << "the size of hash map is: " << hashmap.size() << endl; 
    // 8. iterate the hash map
    for (auto it = hashmap.begin(); it != hashmap.end(); ++it) {
        cout << "(" << it->first << "," << it->second << ") ";
    }
    cout << "are in the hash map." << endl;
    // 9. clear the hash map
    hashmap.clear();
    // 10. check if the hash map is empty
    if (hashmap.empty()) {
        cout << "hash map is empty now!" << endl;
    }
}

Python3

  • 字典是一种可变容器模型,且可存储任意类型对象。
  • 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中
  • 键必须是唯一的,但值则不必。
  • 值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
#【1】初始化
hashmap = {}
hashmap = {0 : 0, 2 : 3}
#【2】添加、更新键值对
hashmap[1] = 1#插入一个新的键值对或更新已有的键的值
hashmap.update(dic2)#字典参数 dict2 的 key/value(键/值) 对添加到字典
hashmap.setdefault(3, default=None)#获取指定key的值,但如果键不存在将会添加键并将值设为default 
#【3】通过key访问值
print("The value of key 1 is: " + str(hashmap[1]))
hashmap.get(1, default=None)#返回指定键的值,如果值不在字典中返回default值
#【4】删除
del hashmap[2]#删除一个key
hashmap.pop(2)#删除字典key对应的值,key也会被删除。返回值为被删除的值。key值必须给出。 否则,返回default值。
hashmap.popitem()#删除字典中的最后一对键和值,并返回删除的键和值。
#【5】检查键是否存在
print(2 in hashmap)
#【6】两个键和值可以拥有不同数据类型
hashmap["pi"] = 3.1415
#【7】获取字典大小
print("The size of hash map is: " + str(len(hashmap)))
#【8】遍历字典
for key in hashmap:
    print("(" + str(key) + "," + str(hashmap[key]) + ")", end=" ")
print("are in the hash map.")
#【9】获取字典所有键、值、所有键值对
hashmap.keys()#返回一个迭代器,可以使用 list() 来转换为列表
hashmap.values()#返回一个迭代器,可以使用 list() 来转换为列表
hashmap.items()#以列表返回可遍历的(键, 值) 元组数组
str(hashmap)#输出字典,以可打印的字符串表示。
#【10】清空字典
hashmap.clear()
del hasnmap
#【11】复制字典
hashmap.copy()#返回一个字典的浅复制。
#【12】排序
#sorted(iterable, cmp=None, key=None, reverse=False) 
#key的参数为一个函数或者lambda函数
ret1 = sorted(hashmap)#默认对key从小到大排序,返回的是排序后的key列表
ret2 = sorted(dict1.values())#对values进行排序,返回排序后的values列表
ret3 = sorted(hashmap, key=operator.itemgetter(1))#itemgetter:0是key,1是value。返回排序后的items的tuple的list
发布了195 篇原创文章 · 获赞 59 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_36622009/article/details/104857214