Namespaces
Variants

std:: map

From cppreference.net
定义于头文件 <map>
template <

class Key,
class T,
class Compare = std:: less < Key > ,
class Allocator = std:: allocator < std:: pair < const Key, T >>

> class map ;
(1)
namespace pmr {

template <
class Key,
class T,
class Compare = std:: less < Key >
> using map = std :: map < Key, T, Compare,
std:: pmr :: polymorphic_allocator < std:: pair < const Key, T >>> ;

}
(2) (C++17 起)

std::map 是一个已排序的关联容器,包含具有唯一键的键值对。键通过比较函数 Compare 进行排序。搜索、删除和插入操作具有对数复杂度。映射通常实现为 Red–black trees (红黑树)。

std::map 的迭代器按键值升序遍历,其中升序由构造时使用的比较函数定义。也就是说,给定

  • m ,一个 std::map
  • it_l it_r ,可解引用的 m 迭代器,且满足 it_l < it_r

m. value_comp ( ) ( * it_l, * it_r ) == true (若使用默认比较函数则为升序排列)。

标准库在所有使用 Compare 要求的地方,唯一性都是通过等价关系来确定的。不严格地说,两个对象 a b 如果彼此都不小于对方(即 ! comp ( a, b ) && ! comp ( b, a ) ),则它们被视为等价的(非唯一的)。

std::map 满足 Container AllocatorAwareContainer AssociativeContainer ReversibleContainer 的要求。

std::map 的所有成员函数均为 constexpr :可以在常量表达式的求值过程中创建和使用 std::map 对象。

然而, std::map 对象通常不能是 constexpr ,因为任何动态分配的存储必须在同一常量表达式求值期间释放。

(自 C++26 起)

目录

模板参数

成员类型

类型 定义
key_type Key
mapped_type T
value_type std:: pair < const Key, T >
size_type 无符号整数类型(通常为 std::size_t
difference_type 有符号整数类型(通常为 std::ptrdiff_t
key_compare Compare
allocator_type Allocator
reference value_type &
const_reference const value_type &
pointer

Allocator::pointer

(C++11 前)

std:: allocator_traits < Allocator > :: pointer

(C++11 起)
const_pointer

Allocator::const_pointer

(C++11 前)

std:: allocator_traits < Allocator > :: const_pointer

(C++11 起)
iterator LegacyBidirectionalIterator 且为 ConstexprIterator (C++26 起) 指向 value_type
const_iterator LegacyBidirectionalIterator 且为 ConstexprIterator (C++26 起) 指向 const value_type
reverse_iterator std:: reverse_iterator < iterator >
const_reverse_iterator std:: reverse_iterator < const_iterator >
node_type (C++17 起) 表示容器节点的 node handle 特化
insert_return_type (C++17 起) 描述插入 node_type 结果的类型,

template < class Iter, class NodeType >
struct /*未指定*/
{
Iter     position ;
bool inserted ;
NodeType node ;
} ;

使用模板参数 iterator node_type 实例化的特化。

成员类

比较 value_type 类型的对象
(类)

成员函数

构造 map
(公开成员函数)
销毁 map
(公开成员函数)
为容器赋值
(公开成员函数)
返回关联的分配器
(公开成员函数)
元素访问
访问指定元素(带边界检查)
(公开成员函数)
访问或插入指定元素
(公开成员函数)
迭代器
返回指向起始位置的迭代器
(公开成员函数)
(C++11)
返回指向末尾的迭代器
(公开成员函数)
返回指向起始的反向迭代器
(公开成员函数)
(C++11)
返回指向末尾的反向迭代器
(公开成员函数)
容量
检查容器是否为空
(公开成员函数)
返回元素数量
(公开成员函数)
返回可能容纳的最大元素数
(公开成员函数)
修饰符
清空内容
(公开成员函数)
插入元素 或节点 (C++17 起)
(公开成员函数)
插入元素范围
(公开成员函数)
插入元素,若键已存在则赋值给当前元素
(公开成员函数)
(C++11)
原地构造元素
(公开成员函数)
使用提示原位构造元素
(公开成员函数)
若键不存在则就地插入,若键存在则不执行任何操作
(公开成员函数)
删除元素
(公开成员函数)
交换内容
(公开成员函数)
(C++17)
从容器中提取节点
(公开成员函数)
(C++17)
从另一容器接合节点
(公开成员函数)
查找
返回匹配特定键的元素数量
(公开成员函数)
根据特定键查找元素
(公开成员函数)
(C++20)
检查容器是否包含特定键对应的元素
(公开成员函数)
返回匹配特定键的元素范围
(公开成员函数)
返回指向首个 不小于 给定键的元素的迭代器
(公开成员函数)
返回指向首个 大于 给定键的元素的迭代器
(公开成员函数)
观察器
返回用于比较键的函数
(公开成员函数)
返回用于比较 value_type 类型对象中键的函数
(公开成员函数)

非成员函数

(在 C++20 中移除) (在 C++20 中移除) (在 C++20 中移除) (在 C++20 中移除) (在 C++20 中移除) (C++20)
按字典序比较两个 map 的值
(函数模板)
特化 std::swap 算法
(函数模板)
擦除满足特定条件的所有元素
(函数模板)

推导指引

(C++17 起)

注释

功能测试 标准 功能
__cpp_lib_containers_ranges 202202L (C++23) 容器的范围构造与插入
__cpp_lib_constexpr_map 202502L (C++26) constexpr std::map

示例

#include <iostream>
#include <map>
#include <string>
#include <string_view>
void print_map(std::string_view comment, const std::map<std::string, int>& m)
{
    std::cout << comment;
    // 使用 C++17 特性进行迭代
    for (const auto& [key, value] : m)
        std::cout << '[' << key << "] = " << value << "; ";
// C++11 替代方案:
//  for (const auto& n : m)
//      std::cout << n.first << " = " << n.second << "; ";
//
// C++98 替代方案:
//  for (std::map<std::string, int>::const_iterator it = m.begin(); it != m.end(); ++it)
//      std::cout << it->first << " = " << it->second << "; ";
    std::cout << '\n';
}
int main()
{
    // 创建包含三个(字符串,整数)对的映射
    std::map<std::string, int> m{{"CPU", 10}, {"GPU", 15}, {"RAM", 20}};
    print_map("1) 初始映射: ", m);
    m["CPU"] = 25; // 更新现有值
    m["SSD"] = 30; // 插入新值
    print_map("2) 更新后的映射: ", m);
    // 对不存在的键使用 operator[] 总是执行插入操作
    std::cout << "3) m[UPS] = " << m["UPS"] << '\n';
    print_map("4) 更新后的映射: ", m);
    m.erase("GPU");
    print_map("5) 擦除后: ", m);
    std::erase_if(m, [](const auto& pair){ return pair.second > 25; });
    print_map("6) 擦除后: ", m);
    std::cout << "7) m.size() = " << m.size() << '\n';
    m.clear();
    std::cout << std::boolalpha << "8) 映射为空: " << m.empty() << '\n';
}

输出:

1) 初始映射: [CPU] = 10; [GPU] = 15; [RAM] = 20;
2) 更新后的映射: [CPU] = 25; [GPU] = 15; [RAM] = 20; [SSD] = 30;
3) m[UPS] = 0
4) 更新后的映射: [CPU] = 25; [GPU] = 15; [RAM] = 20; [SSD] = 30; [UPS] = 0;
5) 擦除后: [CPU] = 25; [RAM] = 20; [SSD] = 30; [UPS] = 0;
6) 擦除后: [CPU] = 25; [RAM] = 20; [UPS] = 0;
7) m.size() = 3
8) 映射为空: true

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 适用版本 发布行为 正确行为
LWG 230 C++98 Key 未要求满足 CopyConstructible
(可能无法构造 Key 类型的键)
Key 同时需要满足
CopyConstructible 要求
LWG 464 C++98 通过键访问 const map 不够便利 提供 at 成员函数

另请参阅

按键排序的键值对集合
(类模板)
通过哈希存储的键值对集合,键具有唯一性
(类模板)
(C++23)
适配两个容器以提供按键排序的键值对集合,键具有唯一性
(类模板)