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

【cpp Trip第2站】map,set,hash

C++中的map、set和hash容器介绍

1. map(映射)

map是C++ STL中的关联容器,它存储键值对(key-value pairs),并根据键自动排序。

特点:

• 基于红黑树实现,保持元素有序

• 键唯一(不允许重复)

• 查找、插入、删除操作的时间复杂度为O(log n)

基本用法:

#include
#include

std::map<std::string, int> ageMap;

// 插入元素
ageMap[“Alice”] = 25;
ageMap[“Bob”] = 30;
ageMap.insert({“Charlie”, 28});

// 访问元素
int aliceAge = ageMap[“Alice”];

// 遍历
for (const auto& pair : ageMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}

// 查找
if (ageMap.find(“Bob”) != ageMap.end()) {
std::cout << “Bob found!” << std::endl;
}

变体:

• multimap:允许键重复的map

• unordered_map:基于哈希表的map,不排序,查找更快(平均O(1))

2. set(集合)

set是存储唯一元素的容器,元素自动排序。

特点:

• 基于红黑树实现

• 元素唯一且有序

• 查找、插入、删除操作的时间复杂度为O(log n)

基本用法:

#include

std::set numbers;

// 插入元素
numbers.insert(3);
numbers.insert(1);
numbers.insert(4);

// 遍历
for (int num : numbers) {
std::cout << num << " ";
}

// 查找
if (numbers.find(3) != numbers.end()) {
std::cout << “3 found in set” << std::endl;
}

变体:

• multiset:允许元素重复的set

• unordered_set:基于哈希表的set,不排序,查找更快(平均O(1))

3. hash(哈希表)

在C++中,哈希表主要通过unordered_map和unordered_set实现。

特点:

• 基于哈希表实现

• 元素无序

• 平均情况下查找、插入、删除操作的时间复杂度为O(1)

• 最坏情况下(哈希冲突严重)可能退化为O(n)

基本用法:

unordered_map:

#include <unordered_map>
#include

std::unordered_map<std::string, int> phonebook;

phonebook[“Alice”] = 123456;
phonebook[“Bob”] = 987654;

// 访问
int aliceNumber = phonebook[“Alice”];

// 遍历
for (const auto& entry : phonebook) {
std::cout << entry.first << ": " << entry.second << std::endl;
}

unordered_set:

#include <unordered_set>

std::unordered_setstd::string names;

names.insert(“Alice”);
names.insert(“Bob”);

if (names.count(“Alice”) > 0) {
std::cout << “Alice is in the set” << std::endl;
}

比较与选择

特性 map/set unordered_map/unordered_set

实现方式 红黑树 哈希表

元素顺序 有序 无序

查找性能 O(log n) 平均O(1),最坏O(n)

内存使用 通常较少 通常较多

适用场景 需要有序访问 需要快速查找,不关心顺序

选择建议:

• 如果需要元素有序,使用map/set

• 如果只需要快速查找,不关心顺序,使用unordered_map/unordered_set

• 如果数据量很大且哈希函数良好,哈希表通常性能更好


文章转载自:

http://P9vVr3gY.gmgyt.cn
http://3klxL8by.gmgyt.cn
http://1Zn4aOHo.gmgyt.cn
http://COxxZHvQ.gmgyt.cn
http://QioQhgxo.gmgyt.cn
http://EddCP52u.gmgyt.cn
http://SiBnXSjg.gmgyt.cn
http://U1AzhFzV.gmgyt.cn
http://lmcvH91v.gmgyt.cn
http://a7ISDgiN.gmgyt.cn
http://o5zxhKRV.gmgyt.cn
http://i8uLNMWF.gmgyt.cn
http://khbolhGQ.gmgyt.cn
http://4t4roL1b.gmgyt.cn
http://Ja94SGmB.gmgyt.cn
http://fCgRdX0Y.gmgyt.cn
http://crpK8zrx.gmgyt.cn
http://RowETBHw.gmgyt.cn
http://Up7M2SFq.gmgyt.cn
http://bDjIDEga.gmgyt.cn
http://qy0AdVI8.gmgyt.cn
http://1A07ME8Q.gmgyt.cn
http://6z4sxsuG.gmgyt.cn
http://nogkiyJl.gmgyt.cn
http://AyoCmhHJ.gmgyt.cn
http://6kWDKfWH.gmgyt.cn
http://Vs6gRhaG.gmgyt.cn
http://poAGwMqf.gmgyt.cn
http://UQPvEZ5Z.gmgyt.cn
http://LDiMwS9I.gmgyt.cn
http://www.dtcms.com/a/385473.html

相关文章:

  • 菊风携手东莞银行,推进金融信创国产化进程
  • 内部类的用法
  • 设计模式(C++)详解—适配器模式(2)
  • 6.Cesium 学习
  • 拉氏变换的 s 域微分性质
  • 掌握Scrapy数据建模与请求技巧
  • LLaMA Factory微调记录(重修版)
  • JAVA开发面试题
  • 逆向国内外社媒电商爬虫算法思路
  • 中山AI搜索优化公司:AI时代GEO技术全解析
  • PostgreSQL GIN 索引揭秘
  • 老鸟对单片机全局变量常用用法(读写在2个独立函数中)
  • 大前端社交应用中 AI 驱动的内容审核与反垃圾信息机制
  • MP3的ID3信息简介及其如何解析
  • MyBatis-Plus 扩展全局方法
  • java中的泛型
  • 使用 AWS Comprehend 综合指南
  • 使用秩和比拟解决非独立同分布情况下的投毒攻击
  • 七、vue3后台项目系列——包装scss、全句变量scss与导入
  • 煤矿山井下绝绝缘监测故障定位
  • 海外分部人员OA请假申请时长为0
  • MySQL --JDBC
  • python使用pyodbc通过不同认证方式连接sqlserver数据源
  • java通过线程池加CompletableFuture实现批量异步处理
  • Coze源码分析-资源库-创建知识库-后端源码-详细流程梳理
  • 极简版 Nginx 反向代理实验步骤
  • python-86-基于Graphviz或Mermaid绘制流程图
  • 智能农机无人驾驶作业套圈路径规划
  • Rayon Rust中的数据并行库入门教程
  • NumPy数组与Python列表的赋值行为解析