亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C++ std::map 完全指南:键值对容器、核心操作与实战代码

什么是 std::map?

在 C++ 标准模板库(STL)中,std::map 是一种关联容器,以键值对存储元素。每个键唯一,容器按键的比较函数(默认 std::less)自动保持元素有序。值可为任意类型,通过对应键访问。

为什么使用 std::map?

语法

声明 map:

std::map<KeyType, ValueType> mapName;

示例:

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

成员类型

  • key_type:键类型。
  • mapped_type:映射值类型。
  • value_type:std::pair<const key_type, mapped_type>
  • iterator:双向迭代器遍历 value_type。
  • const_iterator、reverse_iterator、const_reverse_iterator
  • size_type、difference_type

内置函数

  • begin() – 首元素迭代器。
  • end() – 尾后迭代器。
  • size() – 元素数量。
  • empty() – 是否为空。
  • insert() – 插入键值对。
  • find() – 按键查找元素。
  • erase() – 按迭代器或键删除元素。
  • clear() – 清空所有元素。
  • 其他工具如 count() 和 lower_bound() 亦可用。

遍历 map 元素

迭代器提供遍历顺序的完全控制,始终按键排序。

#include <iostream> #include <string> #include <map> using namespace std; int main() { map<int, string> students; students.insert({200, "Alice"}); students.insert({201, "John"}); cout << "Map size is: " << students.size() << endl; cout << endl << "Default map order:" << endl; for (auto it = students.begin(); it != students.end(); ++it) { cout << it->first << ": " << it->second << endl; } return 0; }

输出:

C++ std::map 完全指南:键值对容器、核心操作与实战代码

向 std::map 插入数据

新条目用 insert();静默拒绝重复键。覆盖现有键用 insert_or_assign()。

#include <map> #include <iostream> using namespace std; int main() { map<int, int> m{{1,3},{2,4},{3,5}}; m.insert({5,6}); // 新增键值对 m.insert({1,8}); // 忽略,键 1 已存在 m.insert_or_assign(1,6); // 更新键 1 的值为 6 cout << "Key\tElement\n"; for (auto& p : m) { cout << p.first << '\t' << p.second << '\n'; } return 0; }

输出:

C++ std::map 完全指南:键值对容器、核心操作与实战代码

在 map 中查找

用 find() 查找键。存在则返回指向元素的迭代器;否则返回 end()。

#include <iostream> #include <map> using namespace std; int main() { map<int, string> students; students.insert({200, "Alice"}); students.insert({201, "John"}); if (auto it = students.find(201); it != students.end()) { cout << "Key 201 has value: " << it->second << endl; } return 0; }

输出:

C++ std::map 完全指南:键值对容器、核心操作与实战代码

从 map 删除数据

可按键或迭代器删除元素。erase() 仅使指向被删元素的迭代器失效。

#include <iostream> #include <map> using namespace std; int main() { map<string, int> myMap; myMap["cow"] = 1; myMap["cat"] = 2; myMap["lion"] = 3; if (auto it = myMap.find("cat"); it != myMap.end()) { myMap.erase(it); } for (auto& p : myMap) { cout << p.first << ": " << p.second << endl; } return 0; }

输出:

C++ std::map 完全指南:键值对容器、核心操作与实战代码

总结

  • std::map 是键唯一的有序关联容器。
  • 键提供高效查找;值存储关联数据。
  • 核心操作均为对数时间,得益于底层红黑树。
  • 迭代器支持遍历、插入、删除和范围查询。
  • insert_or_assign() 简化值更新。
  • find() 快速检查存在性并直接访问。
  • 擦除操作可针对单元素或清空整个 map。

C语言

  1. 精通C标准库函数:实战指南
  2. C++ 运算符全解析:类型、示例与示例程序
  3. C++ do-while 循环详解:语法、实战示例与嵌套循环
  4. 掌握 C++ switch-case:语法、用法与实战示例
  5. C++ 动态数组分配实战指南:new/delete、初始化、扩容与内存管理
  6. C++ 指针精通:核心概念、代码示例与实战应用
  7. C++ 运算符重载实战指南:语法、方法、限制与代码示例
  8. C++ 结构体详解:定义、初始化、指针、函数参数与局限性
  9. 掌握 C++ std::list:语法、函数与实战示例
  10. C++ 函数详解:从声明到调用的实战指南
  11. C++ Struct 与 Class 核心差异、语法及最佳实践
  12. C 语言 calloc() 详解:零初始化动态分配原理与实战案例