C++ std:map的使用方法
作者:木宇(记得热爱生活)
std::map 是 C++ 标准库中一个强大而高效的关联容器,提供键-值对存储、自动排序以及高效查找的功能。在实际开发中,std::map 在需要有序存储和快速检索的场景中非常实用。本篇文章将详细解析 std::map 的功能和用法,并配有代码示例,帮助你更好地掌握它。
1. 什么是 std::map
std::map 是 C++ STL(Standard Template Library中的一种关联容器,提供了键值对的存储形式,其中每个键是唯一的并且按顺序排列。具体来说:
- 键(Key):每个键是唯一的,并用于查找特定的值。
- 值(Value):键所对应的内容,即要存储的数据
为什么使用 std::map
- 快速查找:std::map 使用平衡二叉树(通常为红黑树)实现,具有O(logn) 的查找、插入和删除效率。
- 自动排序:std::map 会自动按键升序排列,省去了手动排序的麻烦。
- 键唯一性:同一 map 中不允许有重复的键,这使它适合存储唯一的键-值对。
2. std::map 的基本用法
2.1 定义和初始化 std::map
std::map 可以通过多种方式进行定义和初始化。常见的定义方法如下:
#include <iostream> #include <map> using namespace std; int main() { // 定义一个键为 int,值为 string 的 map map<int, string> studentMap; // 使用初始化列表直接初始化 map map<int, string> employeeMap = { {1, "Alice"}, {2, "Bob"}, {3, "Charlie"} }; return 0; }
2.2 插入元素
std::map 提供了两种插入方式:使用 insert 方法和 [ ] 运算符。
map<int, string> studentMap; // 方法 1:使用 insert 插入键值对 studentMap.insert({1, "Alice"}); // 方法 2:使用 [] 运算符直接插入 studentMap[2] = "Bob"; // 输出所有键值对 for (const auto &entry : studentMap) { cout << entry.first << ": " << entry.second << endl; }
- 使用 [ ] 操作符的插入方式更方便,而且如果键已存在,则会更新对应的值。
2.3 查找元素
使用 find 函数可以查找特定的键。如果找到则返回一个指向该元素的迭代器,否则返回 map::end() 迭代器。
auto 是 C++11 引入的一种自动类型推导关键字,用于让编译器自动推导变量的类型。
auto it = studentMap.find(1); // 查找键为 1 的元素 if (it != studentMap.end()) { cout << "Found: " << it->second << endl; } else { cout << "Not found!" << endl; }
2.4 删除元素
std::map 提供了多种删除方式:按键删除、按迭代器删除、删除特定范围的元素。
// 按键删除 studentMap.erase(1); // 按迭代器删除 auto it = studentMap.find(2); if (it != studentMap.end()) { studentMap.erase(it); }
3. std::map 常用函数详解
3.1 大小与检查是否为空:size 和 empty
- size:返回 map 中的键值对数量。
- empty:判断 map 是否为空。
cout << "Size: " << studentMap.size() << endl; if (studentMap.empty()) { cout << "Map is empty" << endl; }
3.2 遍历 std::map
遍历 map 的常见方式是使用范围基于 for 循环或迭代器。
// 使用范围基于 for 循环 for (const auto &entry : studentMap) { cout << entry.first << ": " << entry.second << endl; } // 使用迭代器 for (auto it = studentMap.begin(); it != studentMap.end(); ++it) { cout << it->first << ": " << it->second << endl; }
3.3 统计键的数量:count
count 函数返回特定键的数量,对于 std::map 来说,键是唯一的,因此结果要么是 0,要么是 1。
if (studentMap.count(2) > 0) { cout << "Key 2 exists" << endl; }
3.4 获取范围:lower_bound 和 upper_bound
- lower_bound:返回第一个不小于指定键的迭代器。
- upper_bound:返回第一个大于指定键的迭代器。
map<int, string> employeeMap = { {1, "Alice"}, {3, "Bob"}, {5, "Charlie"} }; // 获取范围 auto lb = employeeMap.lower_bound(3); // 指向键 3 的位置 auto ub = employeeMap.upper_bound(3); // 指向键 5 的位置 if (lb != employeeMap.end()) { cout << "Lower bound: " << lb->first << endl; } if (ub != employeeMap.end()) { cout << "Upper bound: " << ub->first << endl; }
3.5 清空 map
clear 函数清空 map 中的所有元素。
studentMap.clear(); if (studentMap.empty()) { cout << "Map is now empty" << endl; }
3.6 排序
std::map 的一个重要特性是自动排序,即在插入新元素后,map 会按键的升序排列。我们无需手动排序或管理顺序,std::map 会始终保持内部的有序性。下面我们来看看如何体现这一特性,以及实际示例展示 std::map 的自动排序。
3.6.1 自动排序示例
当我们往 map 中插入元素时,map 会自动按键升序排列存储。即使插入顺序是随机的,遍历时键值对的输出顺序依旧是有序的。
#include <iostream> #include <map> using namespace std; int main() { map<int, string> studentMap; // 按随机顺序插入元素 studentMap[3] = "Charlie"; studentMap[1] = "Alice"; studentMap[4] = "Diana"; studentMap[2] = "Bob"; // 遍历并打印元素,观察输出顺序 cout << "学生名单 (按学号升序自动排序):" << endl; for (const auto &entry : studentMap) { cout << "学号: " << entry.first << ", 姓名: " << entry.second << endl; } return 0; }
3.6.2 自定义排序(高级用法)
除了默认的升序排列,std::map 还允许自定义排序。可以在定义 map 时,通过传入自定义比较函数,实现按降序或其他规则排序。
#include <iostream> #include <map> #include <string> using namespace std; // 自定义比较函数,按降序排序 struct DescendingOrder { bool operator()(const int &a, const int &b) const { return a > b; } }; int main() { map<int, string, DescendingOrder> studentMap; // 插入元素 studentMap[3] = "Charlie"; studentMap[1] = "Alice"; studentMap[4] = "Diana"; studentMap[2] = "Bob"; // 输出 map 的内容 cout << "学生名单 (按学号降序排序):" << endl; for (const auto &entry : studentMap) { cout << "学号: " << entry.first << ", 姓名: " << entry.second << endl; } return 0; }
在这里,我们定义了一个比较函数 DescendingOrder,并将其作为 map 的第三个模板参数传入,使得 map 按键降序排列。这样,我们就可以轻松调整 map 的排序方式。
4. std::map 使用注意事项
性能考虑std::map 使用平衡二叉树实现,因此查找、插入和删除的时间复杂度为 O(logn)。如果你的数据需要频繁插入或删除,并且对键的顺序不敏感,可以考虑 unordered_map,其时间复杂度为 O(1)。
默认构造键值对当使用 [] 运算符查找键时,如果键不存在,std::map 会自动插入该键并设置为默认值。这在无意中访问不存在的键时可能导致错误。
map<int, int> numbers; numbers[5] += 1; // 如果键 5 不存在,则会创建并初始化为 0,然后加 1
5. 经典示例:统计单词频率
下面是一个使用 std::map 统计文本中单词出现频率的示例:
#include <iostream> #include <map> #include <string> #include <sstream> using namespace std; int main() { map<string, int> wordCount; string text = "this is a sample text with sample words and sample frequencies"; stringstream ss(text); // 将字符串 text 放入 stringstream 中 string word; while (ss >> word) { // 从 ss 中逐个提取单词并存储到 word wordCount[word]++; // 统计每个单词的出现次数 } for (const auto &entry : wordCount) { cout << entry.first << ": " << entry.second << endl; } return 0; }
到此这篇关于C++ std:map的使用方法的文章就介绍到这了,更多相关C++ std:map使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!