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

C++ Map 和 Set 详解:从原理到实战应用

目录

1. 引言

2. Map 详解

2.1 Map 的基本概念

2.2 Map 的底层实现

2.3 Map 的基本操作

(1)声明与初始化

(2)插入元素

(3)查找元素

(4)遍历 Map

(5)删除元素

3. Set 详解

3.1 Set 的基本概念

3.2 Set 的底层实现

3.3 Set 的基本操作

(1)声明与初始化

(2)插入元素

(3)查找元素

(4)遍历 Set

(5)删除元素

4. Map 和 Set 的对比


1. 引言

在 C++ STL(标准模板库)中,mapset 是两种非常重要的关联容器,它们基于 红黑树(Red-Black Tree) 实现,提供了高效的查找、插入和删除操作。本文将深入探讨它们的 底层原理、使用方法、性能分析,并结合 实际代码示例 进行讲解。

2. Map 详解

2.1 Map 的基本概念

map 是一种 键值对(Key-Value) 存储结构,其中:

  • Key 是唯一的,不能重复。
  • Value 可以重复。
  • 默认按 Key 升序 排序(基于 < 运算符)。

2.2 Map 的底层实现

map 的底层通常采用 红黑树(RB-Tree),保证:

  • 插入、删除、查找 的时间复杂度均为 O(log N)
  • 自动维护 Key 的有序性

2.3 Map 的基本操作

(1)声明与初始化
#include <map>
using namespace std;map<string, int> studentScores;  // Key: string, Value: int
studentScores["Alice"] = 95;
studentScores["Bob"] = 88;
(2)插入元素
studentScores.insert({"Charlie", 90});  // C++11 方式
(3)查找元素
auto it = studentScores.find("Alice");
if (it != studentScores.end()) {cout << "Alice's score: " << it->second << endl;
}
(4)遍历 Map
for (const auto& pair : studentScores) {cout << pair.first << ": " << pair.second << endl;
}
(5)删除元素
studentScores.erase("Bob");  // 按 Key 删除
auto it = studentScores.find("Charlie");
if (it != studentScores.end()) {studentScores.erase(it);  // 按迭代器删除
}

3. Set 详解

3.1 Set 的基本概念

set 是一种 唯一元素集合,特点:

  • 元素唯一,不允许重复。
  • 默认按 升序 排序。

3.2 Set 的底层实现

set 同样基于 红黑树,保证:

  • 插入、删除、查找 的时间复杂度为 O(log N)
  • 自动去重并排序。

3.3 Set 的基本操作

(1)声明与初始化
#include <set>
using namespace std;set<int> numbers = {3, 1, 4, 1, 5};  // 自动去重并排序:{1, 3, 4, 5}
(2)插入元素
numbers.insert(2);  // {1, 2, 3, 4, 5}
numbers.emplace(6); // 更高效
(3)查找元素
auto it = numbers.find(3);
if (it != numbers.end()) {cout << "Found: " << *it << endl;
}
(4)遍历 Set
for (int num : numbers) {cout << num << " ";
}
(5)删除元素
numbers.erase(4);  // 按值删除
auto it = numbers.find(2);
if (it != numbers.end()) {numbers.erase(it);  // 按迭代器删除
}

4. Map 和 Set 的对比

特性mapset
存储方式Key-Value 键值对仅存储 Key
查找方式按 Key 查找 Value直接查找元素
去重Key 唯一元素唯一
排序默认按 Key 升序默认按元素升序
底层结构红黑树(RB-Tree)红黑树(RB-Tree)

http://www.lryc.cn/news/586230.html

相关文章:

  • 【Spring AOP】什么是AOP?切点、连接点、通知和切面
  • Python 实战:构建 Git 自动化助手
  • RabbitMQ面试精讲 Day 1:RabbitMQ核心概念与架构设计
  • 网络安全初级第一次作业
  • 医疗AI前端开发中的常见问题分析和解决方法
  • Filament引擎(三) ——引擎渲染流程
  • 【GESP】C++ 2025年6月一级考试-客观题真题解析
  • Apache Iceberg数据湖高级特性及性能调优
  • PyTorch神经网络实战:从零构建图像分类模型
  • 【文献阅读】DEPTH PRO: SHARP MONOCULAR METRIC DEPTH IN LESS THAN A SECOND
  • Rust Web 全栈开发(五):使用 sqlx 连接 MySQL 数据库
  • Spring 框架中的设计模式:从实现到思想的深度解析
  • 单链表的题目,咕咕咕
  • Rust Web 全栈开发(六):在 Web 项目中使用 MySQL 数据库
  • [Python] Flask 多线程绘图时报错“main thread is not in main loop”的解决方案
  • 【字符最长连续数量】2022-8-9
  • wedo稻草人-----第32节(免费分享图纸)
  • windows 改用 nvm
  • hiredis: 一个轻量级、高性能的 C 语言 Redis 客户端库
  • SpringAI实现聊天记录保存到MySQL
  • 浅谈 Python 中的 yield——yield的返回值与send()的关系
  • Golang 面向对象(封装、继承、多态)
  • 特辑:Ubuntu,前世今生
  • Go内存分配
  • python excel处理
  • 【世纪龙科技】新能源汽车结构原理体感教学软件-比亚迪E5
  • Windows 用户账户控制(UAC)绕过漏洞
  • 单细胞分析教程 | (二)标准化、特征选择、降为、聚类及可视化
  • 力扣-24.两两交换链表中的节点
  • 7. 负载均衡:流量调度引擎