C++ std::map

模板: 

template < class Key,                                     // map::key_type
           class T,                                       // map::mapped_type
           class Compare = less<Key>,                     // map::key_compare
           class Alloc = allocator<pair<const Key,T> >    // map::allocator_type
           > class map;

 

定义:

map通过指定的顺序,来存储键值和元素值结合的元素。

在map中,键值经常被用来排序,而且每一个键值在map中是独一无二的。键值和映射值的类型可能不同,并被分组在成员类型value_type中,这是一个组合两者的pair类型:

typedef pair<const Key, T> value_type;

在内部,映射中的元素总是根据其键按照内部比较对象(类型为Compare)所指示的特定严格的弱排序条件进行排序。

map容器通常比unordered_map容器通过它们的键访问单个元素的速度慢,但是它们允许根据它们的顺序对子集进行直接迭代。

映射中的值可以通过对应的键使用括号操作符((operator[])直接访问。

map通常被实现为二叉搜索树。

 

属性:

关联关联容器中的元素由它们的键引用,而不是由它们在容器中的绝对位置引用。
排序容器中的元素始终遵循严格的顺序。所有插入的元素都按这个顺序给定一个位置。
Map每个元素都将一个键关联到一个映射值:键表示标识其主要内容为映射值的元素。
Key容器中的任何两个元素都不能具有相同的键。
Allocator-aware容器使用一个分配器对象来动态地处理它的存储需求。

 

模板参数:

Key

键的类型。映射中的每个元素都由其键值唯一标识。

别名为成员类型映射::key_type。

T

映射值的类型。映射中的每个元素都将一些数据存储为其映射值。

别名为成员类型映射::mapped_type。

Compare

以两个元素键作为参数并返回一个bool值。表达式compare(a,b)中,compare是这种类型的对象,a和b是键值,如果在函数定义的严格弱序中,a被认为在b之前,则表达式comp(a,b)应该返回true。

map对象使用这个表达式来确定元素在容器中的顺序以及两个元素键是否相等(通过反射性地比较它们:如果 !compare(a,b) && !compare(b,a),它们是相等的)。map容器中的任何两个元素都不能具有相同的键。

它可以是函数指针,也可以是函数对象(参见构造函数)。这默认为less<T>,它返回的结果与应用小于操作符(a<b)相同。

别名为成员类型映射::key_compare。

Alloc

用于定义存储分配模型的分配器对象的类型。默认情况下,使用的是分配器类模板,它定义了最简单的内存分配模型,并且是与值无关的。

别名为成员类型映射::allocator_type。

 

成员类型:

C++11:

member typedefinitionnotes
key_typeThe first template parameter (Key) 
mapped_typeThe second template parameter (T) 
value_typepair<const key_type,mapped_type> 
key_compareThe third template parameter (Compare)defaults to: less<key_type>
value_compareNested function class to compare elementssee value_comp
allocator_typeThe fourth template parameter (Alloc)defaults to: allocator<value_type>
referencevalue_type& 
const_referenceconst value_type& 
pointerallocator_traits<allocator_type>::pointerfor the default allocator: value_type*
const_pointerallocator_traits<allocator_type>::const_pointerfor the default allocator: const value_type*
iteratorbidirectional iterator to value_typeconvertible to const_iterator
const_iteratorbidirectional iterator to const value_type 
reverse_iteratorreverse_iterator<iterator> 
const_reverse_iteratorreverse_iterator<const_iterator> 
difference_typea signed integral type, identical to:
iterator_traits<iterator>::difference_type
usually the same as ptrdiff_t
size_typean unsigned integral type that can represent any non-negative value of difference_typeusually the same as size_t

 

成员函数:

(constructor)Construct map (public member function )
(destructor)Construct map (public member function )
operator=Copy container content (public member function )

迭代器:

beginReturn iterator to beginning (public member function )
endReturn iterator to end (public member function )
rbeginReturn reverse iterator to reverse beginning (public member function )
rendReturn reverse iterator to reverse end (public member function )
cbegin Return const_iterator to beginning (public member function )
cend Return const_iterator to end (public member function )
crbegin Return const_reverse_iterator to reverse beginning (public member function )
crend Return const_reverse_iterator to reverse end (public member function )

容量:

emptyTest whether container is empty (public member function )
sizeReturn container size (public member function )
max_sizeReturn maximum size (public member function )

获取元素:

operator[]Access element (public member function )
at Access element (public member function )

修改:

insertInsert elements (public member function )
eraseErase elements (public member function )
swapSwap content (public member function )
clearClear content (public member function )
emplace Construct and insert element (public member function )
emplace_hint Construct and insert element with hint (public member function )

Observers:

key_compReturn key comparison object (public member function )
value_compReturn value comparison object (public member function )

操作:

findGet iterator to element (public member function )
countCount elements with a specific key (public member function )
lower_boundReturn iterator to lower bound (public member function )
upper_boundReturn iterator to upper bound (public member function )
equal_rangeGet range of equal elements (public member function )

Allocator:

get_allocatorGet allocator (public member function )

 

本文内容来源:http://www.cplusplus.com/reference/map/map/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值