【c++】映射表std::map
文章內(nèi)容為網(wǎng)絡(luò)搜集內(nèi)容
std::map
映射表(Map)容器是一個(gè)按特定順序存儲(chǔ)以鍵值對(duì)組合而成的元素的關(guān)聯(lián)容器
// <map> template < class Key,class T,class Compare = less<Key>,class Alloc = allocator<pair<const Key,T> > > class map;- 1
- 2
- 3
- 4
- 5
- 6
- 7
容器特性:
關(guān)聯(lián)(Associative)
關(guān)聯(lián)容器中的元素是通過(guò)主鍵(Key)而不是它們?cè)谌萜髦械慕^對(duì)位置來(lái)引用的。
有序(Ordered)
容器中的元素在任意時(shí)刻都遵循一個(gè)嚴(yán)格排序規(guī)則。所有插入的元素都按該排序規(guī)則獲得對(duì)應(yīng)的位置。
映射(Map)
每個(gè)元素為一個(gè)值(Mapped value)綁定一個(gè)鍵(Key):以主鍵來(lái)標(biāo)志主要內(nèi)容等于被映射值的元素。
鍵唯一(Unique keys)
容器中不存在兩個(gè)元素有相同的主鍵。
能夠感知內(nèi)存分配器的(Allocator-aware)
容器使用一個(gè)內(nèi)存分配器對(duì)象來(lái)動(dòng)態(tài)地處理它的存儲(chǔ)需求。
模板參數(shù)
Key
主鍵的類(lèi)型。
在類(lèi)模板內(nèi)部,使用其別名為 key_type 的成員類(lèi)型。
T
被映射的值的類(lèi)型。
在類(lèi)模板內(nèi)部,使用其別名為 mapped_type 的成員類(lèi)型。
Compare
一個(gè)二元謂詞,以?xún)蓚€(gè)元素的主鍵為參數(shù)返回一個(gè) bool 值。
可以是函數(shù)指針(Function pointer)類(lèi)型或函數(shù)對(duì)象(Function object)類(lèi)型。
在類(lèi)模板內(nèi)部,使用其別名為 key_compare 的成員類(lèi)型。
Alloc
容器內(nèi)部用來(lái)管理內(nèi)存分配及釋放的內(nèi)存分配器的類(lèi)型。
這個(gè)參數(shù)是可選的,它的默認(rèn)值是 std::allocator,這個(gè)是一個(gè)最簡(jiǎn)單的非值依賴(lài)的(Value-independent)內(nèi)存分配器。在類(lèi)模板內(nèi)部,使用其別名為 allocator_type 的成員類(lèi)型。
詳細(xì)說(shuō)明
在一個(gè)映射表容器中,主鍵通常被用來(lái)排序及唯一標(biāo)志一個(gè)元素,而被映射的值保存了與該主鍵關(guān)聯(lián)的內(nèi)容。主鍵與被映射值的類(lèi)型可以不同,在模板內(nèi)部,這兩種類(lèi)型合并綁定成成員類(lèi)型 value_type。由上述描述可知,value_type 是一個(gè)雙元組類(lèi)型(Pair type),具體定義如下:
typedef pair<const Key, T> value_type;- 1
map 容器中的所有元素都是按由類(lèi)型為 Compare 的比較對(duì)象指定的嚴(yán)格弱序規(guī)則排序的。
在用主鍵訪(fǎng)問(wèn)單個(gè)元素時(shí),map 容器通常比 unordered_map 容器低效,但 map 容器允許按順序直接對(duì)某個(gè)子集進(jìn)行迭代。
map 容器通常被實(shí)現(xiàn)為一個(gè)二叉搜索樹(shù)(及其變型),該數(shù)據(jù)結(jié)構(gòu)具有對(duì)數(shù)據(jù)自動(dòng)排序的功能。
在所有關(guān)聯(lián)容器中,map 容器唯一具有的一個(gè)特點(diǎn):實(shí)現(xiàn)了直接訪(fǎng)問(wèn)操作符(operator[]),使得可以直接訪(fǎng)問(wèn)被映射的值。
map 容器支持雙向迭代
成員類(lèi)型
| key_type | 第一個(gè)模板參數(shù) Key |
| mapped_type | 第二個(gè)模板參數(shù) T |
| value_type | std::pair |
| size_type | 無(wú)符號(hào)整數(shù)類(lèi)型(通常為 size_t) |
| difference_type | 有符號(hào)整數(shù)類(lèi)型(通常為 ptrdiff_t) |
| key_compare | 第三個(gè)模板參數(shù) Compare |
| allocator_type | 第四個(gè)模板參數(shù) Alloc |
| reference | Allocator::reference 已棄用 value_type& C++11 |
| const_reference | Allocator::const_reference 已棄用 const value_type& C++11 |
| pointer | Allocator::pointer 已棄用 std::allocator_traits::pointer C++11 |
| const_pointer | Allocator::const_pointer 已棄用 std::allocator_traits::const_pointer C++11 |
| iterator | 雙向迭代器 |
| const_iterator | 常雙向迭代器 |
| reverse_iterator | std::reverse_iterator |
| const_reverse_iterator | std::reverse_iterator |
成員函數(shù)
(constructor) 創(chuàng)建 map (destructor) 釋放 map operator= 值賦操作- 1
- 2
- 3
?Iterators:
begin 返回指向容器起始位置的迭代器(iterator) end 返回指向容器末尾位置的迭代器 rbegin 返回指向容器逆序起始位置的逆序迭代器(reverse_iterator) rend 返回指向容器逆序末尾位置的逆序迭代器 cbegin C++11 返回指向容器起始位置的常迭代器(const_iterator) cend C++11 返回指向容器末尾位置的常迭代器 crbegin C++11 返回指向容器逆序起始位置的常逆序迭代器(const_reverse_iterator) crend C++11 返回指向容器逆序末尾位置的常逆序迭代器- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Capacity:
size 返回有效元素個(gè)數(shù) max_size 返回 map 支持的最大元素個(gè)數(shù) empty 判斷是否為空- 1
- 2
- 3
Element access:
operator[] 訪(fǎng)問(wèn)元素 at C++11 訪(fǎng)問(wèn)元素- 1
- 2
Modifiers:
insert 插入元素 erase 刪除元素 swap 交換內(nèi)容 clear 清空內(nèi)容 emplace C++11 構(gòu)造及插入一個(gè)元素 emplace_hint C++11 按提示構(gòu)造及插入一個(gè)元素- 1
- 2
- 3
- 4
- 5
- 6
Observers:
key_comp 返回鍵比較對(duì)象 value_comp 返回值比較對(duì)象- 1
- 2
Operations:
find 通過(guò)給定主鍵查找元素 count 返回匹配給定主鍵的元素的個(gè)數(shù) lower_bound 返回指向容器中第一個(gè)主鍵等于給定搜索值或在給定搜索值之后的元素的迭代器 upper_bound 返回指向容器中第一個(gè)主鍵在給定搜索值之后的元素的迭代器 equal_range 返回值匹配給定搜索值的元素組成的范圍- 1
- 2
- 3
- 4
- 5
Allocator:
get_allocator 獲得內(nèi)存分配器- 1
非成員函數(shù)
operator==、operator!=、operator<、operator<=、operator>、operator>=- 1
關(guān)系操作符
std::swap 交換兩個(gè)映射表容器的內(nèi)容- 1
算法相關(guān)
搜索算法
std::adjacent_find、std::count、 std::count_if、std::find、 std::find_if、std::find_end、 std::find_first_of、std::search、 std::search_n、std::equal、 std::mismatch- 1
- 2
- 3
- 4
- 5
- 6
二分查找(Binary search)
std::lower_bound、std::upper_bound、 std::equal_range、std::binary_search- 1
- 2
集合(Set)操作
std::includes、std::set_difference、 std::set_intersection、std::set_union、 std::set_symmetric_difference- 1
- 2
- 3
最大與最小
std::min_element、std::max_element- 1
字典序比較
std::lexicographical_compare- 1
排列生成器
std::next_permutation、 std::prev_permutation- 1
- 2
其它操作
std::all_of、std::any_of、std::none_of、 std::for_each、std::copy、std::copy_if、 std::copy_n、std::copy_backward、 ?std::move、std::move_backward、 std::swap_ranges、std::iter_swap、 std::transform、std::replace、 ?std::replace_if、std::replace_copy、 std::replace_copy_if、std::fill、 std::fill_n、std::generate、 std::generate_n、std::remove、 std::remove_if、std::unique、 std::unique_copy、std::reverse、 ?std::reverse_copy、std::rotate、 std::rotate_copy、std::random_shuffle、 std::shuffle、std::partition、 std::is_partitioned、std::stable_partition、 ?std::partition_copy、std::merge- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
代碼示例
示例一:
#include <map> #include <iostream>namespace ClassFoo{ void PrintIntDoubleMap(std::map<int,double>& m, char* pre) {std::map<int,double>::iterator it;std::cout << pre;for ( it = m.begin(); it != m.end(); it++ )std::cout << "(" << it->first << "," << it->second << ") ";std::cout << std::endl; } void MapExample1() {std::map<int,double> foo1;// operator[]在主鍵不存在時(shí),自動(dòng)創(chuàng)建foo1[0] = 32.8;// 普通插入foo1.insert(std::map<int,double>::value_type(1, 33.2));// 帶暗示插入,std::pair<int,double>等價(jià)于上述的// std::map<int,double>::value_typefoo1.insert(foo1.end(),std::pair<int,double>(2,35.8));// 插入一個(gè)范圍std::map<int,double> foo2;foo2.insert(std::map<int,double>::value_type(3, 36.4));foo2.insert(std::map<int,double>::value_type(4, 37.8));foo2.insert(std::map<int,double>::value_type(5, 35.4));foo1.insert(foo2.begin(),foo2.end());PrintIntDoubleMap(foo1,"插入元素后的foo1:");// 查找主鍵4std::map<int,double>::iterator it;it = foo1.find(4);if( it != foo1.end() ){std::cout << "foo1.find(4):";std::cout << "(" << it->first << "," << it->second << ")" << std::endl;}// 刪除上述找到的元素if( it != foo1.end() ){foo1.erase(it);}PrintIntDoubleMap(foo1,"刪除主鍵為4的元素后的foo1:");// 遍歷刪除主鍵為2的元素for(it = foo1.begin();it != foo1.end();it++){//遍歷刪除主鍵等于2//注意,刪除元素會(huì)使迭代范圍發(fā)生變化if(it->first == 2){foo1.erase(it);break;}}PrintIntDoubleMap(foo1,"刪除主鍵為2的元素后的foo1:");foo1.clear();PrintIntDoubleMap(foo1,"清空后的foo1:"); } } int main( ) {ClassFoo::MapExample1();return 0; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
輸出:
插入元素后的foo1:(0,32.8)(1,33.2)(2,35.8)(3,36.4)(4,37.8)(5,35.4) foo1.find(4):(4,37.8) 刪除主鍵為4的元素后的foo1:(0,32.8)(1,33.2)(2,35.8)(3,36.4)(5,35.4) 刪除主鍵為2的元素后的foo1:(0,32.8)(1,33.2)(3,36.4)(5,35.4) 清空后的foo1:- 1
- 2
- 3
- 4
- 5
示例二:c++11
涉及的成員函數(shù) map::at、map::emplace 及 map::emplace_hint 是 C++11 中新增的:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
輸出:
插入元素后的foo1:(0,32.8)(1,33.2)(2,35.8)(3,36.4)(4,37.8)(5,35.4)(6,38)(7,36.4) 修改鍵值為5的元素后的foo1:(0,32.8)(1,33.2)(2,35.8)(3,36.4)(4,37.8)(5,100.1)(6,38)(7,36.4) oo1.find(4):(4,37.8) 刪除主鍵為4的元素后的foo1:(0,32.8)(1,33.2)(2,35.8)(3,36.4)(5,100.1)(6,38)(7,36.4) 刪除主鍵為2的元素后的foo1:(0,32.8)(1,33.2)(3,36.4)(5,100.1)(6,38)(7,36.4) 清空后的foo1:- 1
- 2
- 3
- 4
- 5
- 6
示例三:
主鍵及被映射值都為對(duì)象。下面這個(gè)例子實(shí)現(xiàn)了最簡(jiǎn)單的電話(huà)簿功能:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
總結(jié)
以上是生活随笔為你收集整理的【c++】映射表std::map的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: munin mysql_munin 监控
- 下一篇: 脚手架 - props