命名空间
变体
操作

std::map

来自 cppreference.cn
< cpp‎ | container
 
 
 
 
定义于头文件 <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) (since 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,因为任何动态分配的存储都必须在相同的常量表达式求值中释放。

(since 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

(until C++11)

std::allocator_traits<Allocator>::pointer

(since C++11)
[编辑]
const_pointer

Allocator::const_pointer

(until C++11)

std::allocator_traits<Allocator>::const_pointer

(since C++11)
[编辑]
iterator LegacyBidirectionalIterator ConstexprIterator(since C++26) to value_type[编辑]
const_iterator LegacyBidirectionalIteratorConstexprIterator(since C++26) to const value_type[编辑]
reverse_iterator std::reverse_iterator<iterator>[编辑]
const_reverse_iterator std::reverse_iterator<const_iterator>[编辑]
node_type (since C++17) 一种 节点句柄 的特化,表示容器节点[编辑]
insert_return_type (since C++17) 描述插入 node_type 结果的类型,是以下内容的特化

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

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

[编辑] 成员类

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

[编辑] 成员函数

构造 map
(公有成员函数) [编辑]
析构 map
(公有成员函数) [编辑]
为容器赋值
(公有成员函数) [编辑]
返回关联的分配器
(公有成员函数) [编辑]
元素访问
访问指定的元素,带边界检查
(公有成员函数) [编辑]
访问或插入指定的元素
(公有成员函数) [编辑]
迭代器
返回指向开头的迭代器
(公有成员函数) [编辑]
(C++11)
返回指向末尾的迭代器
(公有成员函数) [编辑]
返回指向开头的逆向迭代器
(公有成员函数) [编辑]
(C++11)
返回指向末尾的逆向迭代器
(公有成员函数) [编辑]
容量
检查容器是否为空
(公有成员函数) [编辑]
返回元素数量
(公有成员函数) [编辑]
返回元素的最大可能数量
(公有成员函数) [编辑]
修改器
清除内容
(公有成员函数) [编辑]
插入元素 或节点(since 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 算法
(函数模板) [编辑]
移除所有满足特定标准的元素
(函数模板) [编辑]

推导指引

(since C++17)

[编辑] 注解

特性测试 Std 特性
__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++ 标准。

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

[编辑] 参见

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