当前位置: 首页 > news >正文

数据结构:哈希表(unordered_map)

unordered_map 是 C++ 标准库中的一种哈希表实现,它提供了基于键值对(key-value)的存储,提供了常数时间复杂度的查找、插入和删除键值对的操作。

初始化代码示例:

#include <unordered_map>
using namespace std;


unordered_map<int, string> hashmap;   // 初始化一个空的哈希表 map


unordered_map<int, string> hashmap{{1, "one"}, {2, "two"}, {3, "three"}};// 初始化一个包含一些键值对的哈希表 map

另外,在C++哈希表中,如果被访问的是一个不存在的键,它会自动创建这个键,对应的值时默认构造的值。

 

#include <iostream>
#include <unordered_map>
using namespace std;

int main() {
    // 初始化哈希表
    unordered_map<int, string> hashmap{{1, "one"}, {2, "two"}, {3, "three"}};


    cout << hashmap.empty() << endl;    // 检查哈希表是否为空,输出:0 (false)


    cout << hashmap.size() << endl;    // 获取哈希表的大小,输出:3

    // 查找指定键是否存在
    // 注意 contains 方法是 C++20 新增的
    // 输出:Key 2 -> two
    if (hashmap.contains(2)) {
        cout << "Key 2 -> " << hashmap[2] << endl;
    } else {
        cout << "Key 2 not found." << endl;
    }


    cout << hashmap[4] << endl;    // 获取指定键对应的值,若不存在会返回默认构造的值
    // 输出空字符串


    hashmap[4] = "four";    // 插入一个新的键值对


    cout << hashmap[4] << endl;    // 获取新插入的值,输出:four


    hashmap.erase(3);    // 删除键值对


    if (hashmap.contains(3)) {
        cout << "Key 3 -> " << hashmap[3] << endl;
    } else {
        cout << "Key 3 not found." << endl;
    }
    // 检查删除后键 3 是否存在
    // 输出:Key 3 not found.

    // 遍历哈希表
    // 输出(顺序可能不同):
    // 4 -> four
    // 2 -> two
    // 1 -> one
    for (const auto &pair: hashmap) {
        cout << pair.first << " -> " << pair.second << endl;
    }

    // 特别注意,访问不存在的键会自动创建这个键
    unordered_map<int, string> hashmap2;

    // 键值对的数量是 0
    cout << hashmap2.size() << endl; // 0

    // 访问不存在的键,会自动创建这个键,对应的值是默认构造的值
    cout << hashmap2[1] << endl; // empty string
    cout << hashmap2[2] << endl; // empty string

    // 现在键值对的数量是 2
    cout << hashmap2.size() << endl; // 2

    return 0;
}

 哈希集合

unordered_set 是 C++ 标准库中的一种哈希集合实现,用于存储不重复的元素,常见使用场景是对元素进行去重。

初始化示例:

#include <unordered_set>
using namespace std;


unordered_set<int> uset;    // 初始化一个空的哈希集合 set


unordered_set<int> uset{1, 2, 3, 4};  // 初始化一个包含一些元素的哈希集合 set

 常用操作示例:

#include <iostream>
#include <unordered_set>
using namespace std;

int main() {

    unordered_set<int> hashset{1, 2, 3, 4};    // 初始化哈希集合


    cout << hashset.empty() << endl;    // 检查哈希集合是否为空,输出:0 (false)


    cout << hashset.size() << endl;    // 获取哈希集合的大小,输出:4

    // 查找指定元素是否存在
    // 输出:Element 3 found.
    if (hashset.contains(3)) {
        cout << "Element 3 found." << endl;
    } else {
        cout << "Element 3 not found." << endl;
    }

    // 插入一个新的元素
    hashset.insert(5);

    // 删除一个元素
    hashset.erase(2);
    // 输出:Element 2 not found.
    if (hashset.contains(2)) {
        cout << "Element 2 found." << endl;
    } else {
        cout << "Element 2 not found." << endl;
    }

    // 遍历哈希集合
    // 输出(顺序可能不同):
    // 1
    // 3
    // 4
    // 5
    for (const auto &element : hashset) {
        cout << element << endl;
    }

    return 0;
}

相关文章:

  • 1.13作业
  • 人工智能之自动驾驶技术体系
  • 大学本科教务系统设计方案,涵盖需求分析、架构设计、核心模块和技术实现要点
  • 长尾关键词优化三步法:提升SEO搜索排名实战
  • MATLAB | 设置滑动窗口计算栅格数据的CV变异系数
  • MySQL数据库表约束详解
  • 一些时间方法
  • python入门 介绍及变量的使用
  • 低代码与开发框架的一些整合[2]
  • Error: error:0308010C:digital envelope routines::unsupported
  • My Metronome for Mac v1.4.2 我的节拍器 支持M、Intel芯片
  • 【工具篇】【深度解析 DeepAI 工具:开启 AI 应用新体验】
  • 机器翻译中的编码器、自注意和解码器
  • c语言中offseto宏
  • 单片机 code RO-data RW-data ZI-data以及OTA学习
  • 在PyTorch中使用插值法来优化卷积神经网络(CNN)所需硬件资源
  • Spring面试题2
  • gitlab 解决双重认证无法登录remote: HTTP Basic: Access denied.
  • webmin配置终端显示样式,模仿UbuntuDesktop终端
  • 25年HVV关于0day的面试题
  • 安徽省委常委、合肥市委书记费高云卸任副省长职务
  • 5年建成强化城市核心功能新引擎,上海北外滩“风景文化都是顶流”
  • 美国再工业化进程需要中国的产业支持
  • 国办印发《关于进一步加强困境儿童福利保障工作的意见》
  • 保证断电、碰撞等事故中车门系统能够开启!隐藏式门把手将迎来强制性国家标准
  • 民生访谈|今年上海还有哪些重要演出展览?场地配套如何更给力?