【c++】映射表std::map
文章內容為網絡搜集內容
std::map
映射表(Map)容器是一個按特定順序存儲以鍵值對組合而成的元素的關聯容器
// <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
容器特性:
關聯(Associative)
關聯容器中的元素是通過主鍵(Key)而不是它們在容器中的絕對位置來引用的。
有序(Ordered)
容器中的元素在任意時刻都遵循一個嚴格排序規則。所有插入的元素都按該排序規則獲得對應的位置。
映射(Map)
每個元素為一個值(Mapped value)綁定一個鍵(Key):以主鍵來標志主要內容等于被映射值的元素。
鍵唯一(Unique keys)
容器中不存在兩個元素有相同的主鍵。
能夠感知內存分配器的(Allocator-aware)
容器使用一個內存分配器對象來動態地處理它的存儲需求。
模板參數
Key
主鍵的類型。
在類模板內部,使用其別名為 key_type 的成員類型。
T
被映射的值的類型。
在類模板內部,使用其別名為 mapped_type 的成員類型。
Compare
一個二元謂詞,以兩個元素的主鍵為參數返回一個 bool 值。
可以是函數指針(Function pointer)類型或函數對象(Function object)類型。
在類模板內部,使用其別名為 key_compare 的成員類型。
Alloc
容器內部用來管理內存分配及釋放的內存分配器的類型。
這個參數是可選的,它的默認值是 std::allocator,這個是一個最簡單的非值依賴的(Value-independent)內存分配器。在類模板內部,使用其別名為 allocator_type 的成員類型。
詳細說明
在一個映射表容器中,主鍵通常被用來排序及唯一標志一個元素,而被映射的值保存了與該主鍵關聯的內容。主鍵與被映射值的類型可以不同,在模板內部,這兩種類型合并綁定成成員類型 value_type。由上述描述可知,value_type 是一個雙元組類型(Pair type),具體定義如下:
typedef pair<const Key, T> value_type;- 1
map 容器中的所有元素都是按由類型為 Compare 的比較對象指定的嚴格弱序規則排序的。
在用主鍵訪問單個元素時,map 容器通常比 unordered_map 容器低效,但 map 容器允許按順序直接對某個子集進行迭代。
map 容器通常被實現為一個二叉搜索樹(及其變型),該數據結構具有對數據自動排序的功能。
在所有關聯容器中,map 容器唯一具有的一個特點:實現了直接訪問操作符(operator[]),使得可以直接訪問被映射的值。
map 容器支持雙向迭代
成員類型
| key_type | 第一個模板參數 Key |
| mapped_type | 第二個模板參數 T |
| value_type | std::pair |
| size_type | 無符號整數類型(通常為 size_t) |
| difference_type | 有符號整數類型(通常為 ptrdiff_t) |
| key_compare | 第三個模板參數 Compare |
| allocator_type | 第四個模板參數 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 |
成員函數
(constructor) 創建 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 返回有效元素個數 max_size 返回 map 支持的最大元素個數 empty 判斷是否為空- 1
- 2
- 3
Element access:
operator[] 訪問元素 at C++11 訪問元素- 1
- 2
Modifiers:
insert 插入元素 erase 刪除元素 swap 交換內容 clear 清空內容 emplace C++11 構造及插入一個元素 emplace_hint C++11 按提示構造及插入一個元素- 1
- 2
- 3
- 4
- 5
- 6
Observers:
key_comp 返回鍵比較對象 value_comp 返回值比較對象- 1
- 2
Operations:
find 通過給定主鍵查找元素 count 返回匹配給定主鍵的元素的個數 lower_bound 返回指向容器中第一個主鍵等于給定搜索值或在給定搜索值之后的元素的迭代器 upper_bound 返回指向容器中第一個主鍵在給定搜索值之后的元素的迭代器 equal_range 返回值匹配給定搜索值的元素組成的范圍- 1
- 2
- 3
- 4
- 5
Allocator:
get_allocator 獲得內存分配器- 1
非成員函數
operator==、operator!=、operator<、operator<=、operator>、operator>=- 1
關系操作符
std::swap 交換兩個映射表容器的內容- 1
算法相關
搜索算法
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[]在主鍵不存在時,自動創建foo1[0] = 32.8;// 普通插入foo1.insert(std::map<int,double>::value_type(1, 33.2));// 帶暗示插入,std::pair<int,double>等價于上述的// std::map<int,double>::value_typefoo1.insert(foo1.end(),std::pair<int,double>(2,35.8));// 插入一個范圍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//注意,刪除元素會使迭代范圍發生變化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
涉及的成員函數 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
示例三:
主鍵及被映射值都為對象。下面這個例子實現了最簡單的電話簿功能:
- 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
總結
以上是生活随笔為你收集整理的【c++】映射表std::map的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: munin mysql_munin 监控
- 下一篇: 脚手架 - props