一、std::map
(1)map
与其适用场景
std::map
是 C++的STL(标准模板库)中的关联容器(Associative Container),底层基于 红黑树(Red-Black Tree) 实现,支持键值对存储,键自动排序,并提供高效的查找、插入和删除操作。
特点:
- 键值对存储:每个元素是
key-value
形式,如{1, "Alice"}
。 - 键唯一:不允许重复键(使用
std::multimap
可允许重复键)。 - 自动排序:键按升序存储(可自定义降序)。
- 底层实现:基于红黑树(Red-Black Tree),
insert
、erase
、find
复杂度为 O(log n)。 - 支持随机访问:可通过
operator[]
访问或修改值。
适用场景:
- 字典存储(Key-Value 映射)(如学生成绩表)。
- 高效查找(比
vector
、list
快)。 - 按键自动排序(如排名表)。
(2)map
vs unordered_map
特性 | map (有序映射) | unordered_map (哈希映射) |
---|---|---|
底层实现 | 红黑树 | 哈希表 |
时间复杂度 | O(log n) | O(1) (最坏 O(n) ) |
是否排序 | 自动排序 | 无序 |
适用场景 | 有序存储,适用于范围查询 | 更快的查找(无序) |
二、std::map
的常用函数
函数 | 作用 |
---|---|
insert({key, value}) | 插入元素(如果 key 存在,不会修改) |
erase(key) | 删除指定键的元素 |
find(key) | 查找键,返回迭代器 |
count(key) | 检查键是否存在(返回 0 或 1 ) |
operator[] | 访问/修改元素 |
size() | 获取元素个数 |
empty() | 判断是否为空 |
clear() | 清空 map |
三、std::map
的基本使用
(1) 插入、访问与遍历
示例:
#include <iostream>
using namespace std;
#include <map>
#include <string>
int main() {
map<int, string> students;
students.insert({ 1, "Alice" });
students.insert({ 3, "Charlie" });
students[2] = "Bob"; // 另一种插入方式
cout << "学生名单:" << endl;
for (const auto s : students)
{
cout << "ID: " << s.first << ", Name: " << s.second << endl;
}
system("pause");
return 0;
}
注意:
insert
函数的返回值是pair<iterator, bool>
,第一个参数是插入元素或已有键对应的迭代器,第二个参数表示是否成功插入元素。- 执行插入操作时,首先检查给定键是否已存在于
map
中,如果键已存在于map
中,则它不会插入map
中,并且迭代器将迭代到现有键返回,否则在map中插入新元素。
(2) 查找键
示例:
#include <iostream>
using namespace std;
#include <map>
#include <string>
int main() {
map<int, string> students = { {1, "Alice"}, {2, "Bob"}, {3, "Charlie"} };
if (students.find(2) != students.end())
{
cout << "找到 ID 2: " << students[2] << endl;
}
else
{
cout << "ID 2 不存在" << endl;
}
system("pause");
return 0;
}
注意:
find(key)
—— 查找键,返回迭代器:若存在,返回指向目标键的迭代器;若不存在,返回map.end()
迭代器。
(3) 删除元素
示例:
#include <iostream>
using namespace std;
#include <map>
#include <string>
int main() {
map<int, string> students = { {1, "Alice"}, {2, "Bob"}, {3, "Charlie"} };
students.erase(2); // 删除 ID=2 的学生
for (const auto& s : students)
{
cout << "ID: " << s.first << ", Name: " << s.second << endl;
}
system("pause");
return 0;
}
(4) 自定义排序(降序)
示例:
#include <iostream>
using namespace std;
#include <map>
#include <string>
int main() {
map<int, string, greater<int>> students = { {1, "Alice"}, {2, "Bob"}, {3, "Charlie"} };
for (const auto& s : students)
{
cout << "ID: " << s.first << ", Name: " << s.second << endl;
}
system("pause");
return 0;
}
注意:
map
的默认排序为升序(set<int> s
),可用std::greater<>
降序排序。
四、std::map
的应用
(1) 统计单词出现次数
示例:
#include <iostream>
using namespace std;
#include <map>
#include <string>
#include <sstream>
int main() {
map<string, int> wordCount;
string text = "apple banana apple orange banana apple";
stringstream ss(text);
string word;
while (ss >> word)
{
wordCount[word]++;
}
for (const auto& w : wordCount)
{
cout << w.first << ": " << w.second << endl;
}
system("pause");
return 0;
}
注意:
<sstream>
是C++ 标准库中的一个组件,它提供了一种方便的方式来处理字符串流(可以像处理流一样处理字符串)。stringstream
是一个流类,允许你将字符串当作输入/输出流来使用,这使得从字符串中读取数据或将数据写入字符串变得非常简单。
(2) 排行榜(按分数排序)
示例:
#include <iostream>
using namespace std;
#include <map>
#include <string>
int main() {
map<int, string, greater<int>> ranking;
ranking[95] = "Alice";
ranking[90] = "Bob";
ranking[100] = "Charlie";
for (const auto& r : ranking)
{
cout << r.first << " 得分: " << r.second << endl;
}
system("pause");
return 0;
}