C++ STL容器之map

版权声明:本文为博主原创文章,博客地址:https://blog.csdn.net/qq_41855420,未经博主允许不得转载。 https://blog.csdn.net/qq_41855420/article/details/89765418

C++ map 容器

定义于头文件 中,申明为template< class Key, class T, class Compare = std::less<Key>, class Allocator = std::allocator<std::pair<const Key, T> >> class map;
std::map 是有序键值对容器,它的元素的键是唯一的。用比较函数 Compare 排序键。搜索、移除和插入操作拥有对数复杂度。 map 通常实现为红黑树。

在每个标准库使用比较 (Compare) 概念的位置,唯一性以等价关系检验。不精确而言,若二个对象 a 与 b 互相比较不小于对方 : !comp(a, b) && !comp(b, a) ,则认为它们等价(非唯一)。

std::map 满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、关联容器 (AssociativeContainer) 和可逆容器 (ReversibleContainer) 的要求。

成员类型

成员类型 定义
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 Allocator::reference (C++11 前) value_type& (C++11 起)
const_reference Allocator::const_reference (C++11 前) const value_type& (C++11 起)
pointer Allocator::pointer (C++11 前) std::allocator_traits::pointer (C++11 起)
const_pointer Allocator::const_pointer (C++11 前) std::allocator_traits::const_pointer (C++11 起)
iterator 双向迭代器 (LegacyBidirectionalIterator)
const_iterator 常双向迭代器
reverse_iterator std::reverse_iterator
const_reverse_iterator std::reverse_iterator<const_iterator>
node_type(C++17 起) 表示容器结点的结点把柄特化
insert_return_type(C++17 起) 描述插入 node_type 结果的类型

成员类

value_compare 比较类型为value_type的对象 (类)

成员函数

(构造函数) 构造 map (公开成员函数)
(析构函数) 析构 map (公开成员函数)
operator= 赋值给容器 (公开成员函数)
get_allocator 返回相关的分配器 (公开成员函数)

元素访问

at(C++11) 访问指定的元素,同时进行越界检查 (公开成员函数)
operator[] 访问或插入指定的元素 (公开成员函数)

迭代器

begin、cbegin 返回指向容器第一个元素的迭代器 (公开成员函数)
end、cend 返回指向容器尾端的迭代器 (公开成员函数)
rbegin、crbegin 返回指向容器最后元素的逆向迭代器 (公开成员函数)
rend、crend 返回指向前端的逆向迭代器 (公开成员函数)

容量

empty 检查容器是否为空 (公开成员函数)
size 返回容纳的元素数 (公开成员函数)
max_size 返回可容纳的最大元素数 (公开成员函数)

修改器

clear 清除内容 (公开成员函数)
insert 插入元素或结点 (C++17 起) (公开成员函数)
insert_or_assign(C++17) 插入元素,或若关键已存在则赋值给当前元素 (公开成员函数)
emplace (C++11) 原位构造元素 (公开成员函数)
emplace_hint (C++11) 使用提示原位构造元素 (公开成员函数)
try_emplace (C++17) 若键不存在则原位插入,若键存在则不做任何事 (公开成员函数)
erase 擦除元素 (公开成员函数)
swap 交换内容 (公开成员函数)
extract (C++17) 从另一容器释出结点 (公开成员函数)
merge (C++17) 从另一容器接合结点 (公开成员函数)

查找

count 返回匹配特定键的元素数量 (公开成员函数)
find 寻找带有特定键的元素 (公开成员函数)
contains (C++20) 检查容器是否含有带特定关键的元素 (公开成员函数)
equal_range 返回匹配特定键的元素范围 (公开成员函数)
lower_bound 返回指向首个不小于给定键的元素的迭代器 (公开成员函数)
upper_bound 返回指向首个大于给定键的元素的迭代器 (公开成员函数)观察器
key_comp 返回用于比较键的函数 (公开成员函数)
value_comp 返回用于在value_type类型的对象中比较键的函数。 (公开成员函数)

猜你喜欢

转载自blog.csdn.net/qq_41855420/article/details/89765418