文章详情 学习记录

C++学习(九)-《C++ 容器类<map>》

发布时间:2026-06-26 16:09 浏览次数:38 最后更新:2026-06-30 14:46

c++、map

正文内容

C++ 容器类 <map>

在 C++ 中,<map> 是标准模板库(STL)的一部分,它提供了一种关联容器,用于存储键值对(key-value pairs)。

map 容器中的元素是按照键的顺序自动排序的,这使得它非常适合需要快速查找和有序数据的场景。

定义和特性

  • 键值对map 存储的是键值对,其中每个键都是唯一的。
  • 排序map 中的元素按照键的顺序自动排序,通常是升序。
  • 唯一性:每个键在 map 中只能出现一次。
  • 双向迭代器map 提供了双向迭代器,可以向前和向后遍历元素。


基本语法

包含头文件:

#include <map>

声明 map 容器:

std::map<key_type, value_type> myMap;
  • key_type 是键的类型。
  • value_type 是值的类型。

插入元素:

myMap[key] = value;

访问元素:

value = myMap[key];

遍历 map:

for (std::map<key_type, value_type>::iterator it = myMap.begin(); it != myMap.end(); ++it) {
    std::cout << it->first << " => " << it->second << std::endl;
}

C++11 及以上标准,遍历部分可以简化为范围 for 循环,代码更简洁:

for (auto &p : m) {
    std::cout << p.first << " : " << p.second << std::endl;
}

实例

下面是一个使用 map 的简单实例,我们将创建一个 map 来存储员工的姓名和他们的年龄,并遍历这个 map 来打印每个员工的姓名和年龄。

#include <iostream>#include <map>
#include <string>

int main() {
    // 创建一个 map 容器,存储员工的姓名和年龄
    std::map<std::string, int> employees;

    // 插入员工信息
    employees["Alice"] = 30;
    employees["Bob"] = 25;
    employees["Charlie"] = 35;

    // 遍历 map 并打印员工信息
    for (std::map<std::string, int>::iterator it = employees.begin(); it != employees.end(); ++it) {
        std::cout << it->first << " is " << it->second << " years old." << std::endl;
    }

    return 0;
}

输出:

Alice is 30 years old.
Bob is 25 years old.
Charlie is 35 years old.

进阶用法

检查键是否存在:

if (myMap.find(key) != myMap.end()) {
    // 键存在
}

删除元素:

myMap.erase(key);

清空 map:

myMap.clear();

获取 map 的大小:

size_t size = myMap.size();

其他方法:

myMap.empty();      // 是否为空
myMap.count("Bob"); // key 是否存在(返回 0 或 1)

自定义排序,默认升序排序,可以用 std::greater 或自定义比较函数:

std::map<int, std::string, std::greater<int>> m;  // 降序

使用自定义比较函数:

#include <map>
#include <string>
#include <functional>

bool myCompare(const std::string& a, const std::string& b) {
    return a < b;
}

int main() {
    std::map<std::string, int, std::function<bool(const std::string&, const std::string&)>> myMap(myCompare);

    // 其他操作...

    return 0;
}

map 是 C++ STL 中一个非常有用的容器,特别适合需要快速查找和有序数据的场景:

#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<std::string, int> scores;

    // 插入
    scores["Alice"] = 90;
    scores["Bob"] = 85;
    scores.insert({"Charlie", 92});

    // 遍历
    for (auto &p : scores) {
        std::cout << p.first << " => " << p.second << std::endl;
    }

    // 查找
    auto it = scores.find("Bob");
    if (it != scores.end()) {
        std::cout << "Bob's score: " << it->second << std::endl;
    }

    // 删除
    scores.erase("Alice");

    std::cout << "Size: " << scores.size() << std::endl;

    return 0;
}

输出:

Alice => 90
Bob => 85
Charlie => 92
Bob's score: 85
Size: 2

正文内容已启用复制保护,代码块与行内代码仍支持复制。

评论区

欢迎参与讨论,评论提交与展示状态会在下方同步更新。

发表评论

提交后将进入人工审核,审核通过后才会展示。

邮箱仅用于必要的联系与基础风控,不会在页面中公开展示。

请文明发言,避免发布广告、链接或敏感内容;短时间内频繁提交会被限制。

隐私提示:提交评论即表示你同意站点为评论展示、审核、防刷与安全风控目的处理你填写的昵称、邮箱和评论内容。

其中邮箱不会公开展示,也不会用于与评论无关的公开用途。

读者留言

还没有评论,来发表第一条评论吧。

共 0 条评论 已展示 0 / 0 条评论
当前还没有评论,来发表第一条评论吧。若评论需要审核,通过后会展示在这里。