模板:
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 type | definition | notes |
---|---|---|
key_type | The first template parameter (Key) | |
mapped_type | The second template parameter (T) | |
value_type | pair<const key_type,mapped_type> | |
key_compare | The third template parameter (Compare) | defaults to: less<key_type> |
value_compare | Nested function class to compare elements | see value_comp |
allocator_type | The fourth template parameter (Alloc) | defaults to: allocator<value_type> |
reference | value_type& | |
const_reference | const value_type& | |
pointer | allocator_traits<allocator_type>::pointer | for the default allocator: value_type* |
const_pointer | allocator_traits<allocator_type>::const_pointer | for the default allocator: const value_type* |
iterator | a bidirectional iterator to value_type | convertible to const_iterator |
const_iterator | a bidirectional iterator to const value_type | |
reverse_iterator | reverse_iterator<iterator> | |
const_reverse_iterator | reverse_iterator<const_iterator> | |
difference_type | a signed integral type, identical to: iterator_traits<iterator>::difference_type |
usually the same as ptrdiff_t |
size_type | an unsigned integral type that can represent any non-negative value of difference_type | usually the same as size_t |
成員函數:
(constructor) | Construct map (public member function ) |
(destructor) | Construct map (public member function ) |
operator= | Copy container content (public member function ) |
迭代器:
begin | Return iterator to beginning (public member function ) |
end | Return iterator to end (public member function ) |
rbegin | Return reverse iterator to reverse beginning (public member function ) |
rend | Return 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 ) |
容量:
empty | Test whether container is empty (public member function ) |
size | Return container size (public member function ) |
max_size | Return maximum size (public member function ) |
獲取元素:
operator[] | Access element (public member function ) |
at | Access element (public member function ) |
修改:
insert | Insert elements (public member function ) |
erase | Erase elements (public member function ) |
swap | Swap content (public member function ) |
clear | Clear 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_comp | Return key comparison object (public member function ) |
value_comp | Return value comparison object (public member function ) |
操作:
find | Get iterator to element (public member function ) |
count | Count elements with a specific key (public member function ) |
lower_bound | Return iterator to lower bound (public member function ) |
upper_bound | Return iterator to upper bound (public member function ) |
equal_range | Get range of equal elements (public member function ) |
Allocator:
get_allocator | Get allocator (public member function ) |