C++ 标准模板库STL中map用法介绍

本文详细介绍了C++11标准模板库STL中的map容器,它是一个有序且键值唯一的关键字对容器,基于红黑树实现。内容涵盖了map的成员类型、成员类和各种成员函数,包括元素访问、迭代器、容量、修改、查找和观察器等操作,帮助读者深入理解并掌握map的使用。
摘要由CSDN通过智能技术生成

本文所介绍的std::map用法基于C++11,std::map定义于头文件<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 通常实现为红黑树其包含成员类型其成员函数如下:

成员类型

成员类型 定义
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 std::allocator_traits<Allocator>::pointer
const_pointer std::allocator_traits<Allocator>::const_pointer
iterator 双向迭代器 (BidirectionalIterator)
const_iterator 双向迭代器
reverse_iterator std::reverse_iterator<iterator>
const_reverse_iterator std::reverse_iterator<const_iterator>

成员类

成员类 作用
value_compare 比较类型为value_type的对象

成员函数

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

1、元素访问
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值