命名空间
变体
操作

std::map

来自 cppreference.cn
< cpp‎ | 容器
 
 
 
 
定义于头文件 <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 进行排序。搜索、删除和插入操作具有对数复杂度。Map 通常实现为红黑树

std::map 的迭代器按键的升序迭代,其中升序由用于构造的比较定义。也就是说,给定

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

m.value_comp()(*it_l, *it_r) == true(如果使用默认比较,则从最小到最大)。

在标准库使用 Compare 要求的所有地方,唯一性都通过等价关系确定。不精确地说,如果两个对象 ab 都不比对方小,则它们被认为是等价的(不唯一):!comp(a, b) && !comp(b, a)

std::map 满足 ContainerAllocatorAwareContainerAssociativeContainerReversibleContainer 的要求。

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 LegacyBidirectionalIteratorConstexprIterator(C++26起)const value_type[编辑]
reverse_iterator std::reverse_iterator<iterator>[编辑]
const_reverse_iterator std::reverse_iterator<const_iterator>[编辑]
node_type (C++17 起) 表示容器节点的节点句柄特化[编辑]
insert_return_type (C++17起) 描述插入 node_type 结果的类型,一个特化

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

用模板参数 iteratornode_type 实例化。[编辑]

[编辑] 成员类

比较 value_type 类型的对象
(类) [编辑]

[编辑] 成员函数

构造 map
(公共成员函数) [编辑]
销毁 map
(公共成员函数) [编辑]
将值赋给容器
(公共成员函数) [编辑]
返回关联的分配器
(公共成员函数) [编辑]
元素访问
访问指定的元素,带边界检查
(公共成员函数) [编辑]
访问或插入指定元素
(公共成员函数) [编辑]
迭代器
(C++11起)
返回指向起始的迭代器
(公共成员函数) [编辑]
(C++11起)
返回指向末尾的迭代器
(公共成员函数) [编辑]
(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_containers 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;
    // Iterate using C++17 facilities
    for (const auto& [key, value] : m)
        std::cout << '[' << key << "] = " << value << "; ";
 
// C++11 alternative:
//  for (const auto& n : m)
//      std::cout << n.first << " = " << n.second << "; ";
//
// C++98 alternative:
//  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()
{
    // Create a map of three (string, int) pairs
    std::map<std::string, int> m{{"CPU", 10}, {"GPU", 15}, {"RAM", 20}};
 
    print_map("1) Initial map: ", m);
 
    m["CPU"] = 25; // update an existing value
    m["SSD"] = 30; // insert a new value
    print_map("2) Updated map: ", m);
 
    // Using operator[] with non-existent key always performs an insert
    std::cout << "3) m[UPS] = " << m["UPS"] << '\n';
    print_map("4) Updated map: ", m);
 
    m.erase("GPU");
    print_map("5) After erase: ", m);
 
    std::erase_if(m, [](const auto& pair){ return pair.second > 25; });
    print_map("6) After erase: ", m);
    std::cout << "7) m.size() = " << m.size() << '\n';
 
    m.clear();
    std::cout << std::boolalpha << "8) Map is empty: " << m.empty() << '\n';
}

输出

1) Initial map: [CPU] = 10; [GPU] = 15; [RAM] = 20;
2) Updated map: [CPU] = 25; [GPU] = 15; [RAM] = 20; [SSD] = 30;
3) m[UPS] = 0
4) Updated map: [CPU] = 25; [GPU] = 15; [RAM] = 20; [SSD] = 30; [UPS] = 0;
5) After erase: [CPU] = 25; [RAM] = 20; [SSD] = 30; [UPS] = 0;
6) After erase: [CPU] = 25; [RAM] = 20; [UPS] = 0;
7) m.size() = 3
8) Map is empty: true

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 230 C++98 不要求 Key可复制构造的 (CopyConstructible)
(类型为 Key 的键可能无法构造)
Key 也要求
可复制构造 (CopyConstructible)
LWG 464 C++98 通过键访问 const map 不方便 提供了 at 函数

[编辑] 参阅

键值对的集合,按键排序
(类模板) [编辑]
键值对的集合,按键哈希,键是唯一的
(类模板) [编辑]
(C++23)
适配两个容器以提供键值对集合,按唯一键排序
(类模板) [编辑]