C++学习笔记(三十一)——map

一、std::map

(1)map与其适用场景

std::map 是 C++的STL(标准模板库)中的关联容器(Associative Container),底层基于 红黑树(Red-Black Tree) 实现,支持键值对存储,键自动排序,并提供高效的查找、插入和删除操作

特点:

  • 键值对存储:每个元素是 key-value 形式,如 {1, "Alice"}
  • 键唯一:不允许重复键(使用 std::multimap 可允许重复键)。
  • 自动排序:键按升序存储(可自定义降序)。
  • 底层实现:基于红黑树(Red-Black Tree)inserterasefind 复杂度为 O(log n)
  • 支持随机访问:可通过 operator[] 访问或修改值。

适用场景:

  • 字典存储(Key-Value 映射)(如学生成绩表)。
  • 高效查找(比 vectorlist 快)
  • 按键自动排序(如排名表)。

(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)检查键是否存在(返回 01
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奕天者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值