日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

【c++】映射表std::map

發(fā)布時(shí)間:2025/3/12 c/c++ 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【c++】映射表std::map 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章內(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)型

成員類(lèi)型定義
key_type第一個(gè)模板參數(shù) Key
mapped_type第二個(gè)模板參數(shù) T
value_typestd::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
referenceAllocator::reference 已棄用 value_type& C++11
const_referenceAllocator::const_reference 已棄用 const value_type& C++11
pointerAllocator::pointer 已棄用 std::allocator_traits::pointer C++11
const_pointerAllocator::const_pointer 已棄用 std::allocator_traits::const_pointer C++11
iterator雙向迭代器
const_iterator常雙向迭代器
reverse_iteratorstd::reverse_iterator
const_reverse_iteratorstd::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_findstd::countstd::count_ifstd::findstd::find_ifstd::find_endstd::find_first_ofstd::searchstd::search_nstd::equalstd::mismatch
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

二分查找(Binary search)

std::lower_boundstd::upper_boundstd::equal_rangestd::binary_search
  • 1
  • 2

集合(Set)操作

std::includesstd::set_differencestd::set_intersectionstd::set_unionstd::set_symmetric_difference
  • 1
  • 2
  • 3

最大與最小

std::min_elementstd::max_element
  • 1

字典序比較

std::lexicographical_compare
  • 1

排列生成器

std::next_permutationstd::prev_permutation
  • 1
  • 2

其它操作

std::all_ofstd::any_ofstd::none_ofstd::for_eachstd::copystd::copy_ifstd::copy_nstd::copy_backward、 ?std::movestd::move_backwardstd::swap_rangesstd::iter_swapstd::transformstd::replace、 ?std::replace_ifstd::replace_copystd::replace_copy_ifstd::fillstd::fill_nstd::generatestd::generate_nstd::removestd::remove_ifstd::uniquestd::unique_copystd::reverse、 ?std::reverse_copystd::rotatestd::rotate_copystd::random_shufflestd::shufflestd::partitionstd::is_partitionedstd::stable_partition、 ?std::partition_copystd::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 中新增的:

#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 MapExample2() {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());// 放置插入foo1.emplace(6,38.0);// 帶暗示的放置插入foo1.emplace_hint(foo1.end(),7,36.4);PrintIntDoubleMap(foo1,"插入元素后的foo1:");// 修改鍵值為5的元素中的被foo1.at(5) = 100.1;PrintIntDoubleMap(foo1,"修改鍵值為5的元素后的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::MapExample2();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
  • 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à)簿功能:

#include <iostream> #include <map> #include <string>namespace ClassFoo{ using namespace std; class name {string str; public:name() {}name(string s) { str = s; }string& get() { return str; }};bool operator<(name a, name b) {return a.get() < b.get(); }class phoneNum {string str; public:phoneNum() { }phoneNum(string s) { str = s; }string& get() { return str; } };void MapExample3() {map<name, phoneNum> directory;directory.insert(pair<name, phoneNum>(name("A"), phoneNum("555-1111")));directory.insert(pair<name, phoneNum>(name("B"), phoneNum("555-2222")));directory.insert(pair<name, phoneNum>(name("C"), phoneNum("555-3333")));directory.insert(pair<name, phoneNum>(name("D"), phoneNum("555-4444")));map<name, phoneNum>::iterator p;p = directory.find(name("A"));if(p != directory.end())cout << "A的電話(huà)號(hào)碼為: " << p->second.get() << endl;elsecout << "未發(fā)現(xiàn)A的電話(huà)號(hào)碼" <<endl; } } int main() {ClassFoo::MapExample3();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

總結(jié)

以上是生活随笔為你收集整理的【c++】映射表std::map的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产综合视频在线观看 | 含羞草一区二区三区 | 成人在线观看免费爱爱 | 色射影院 | 欧美xxxxxhd | www四虎精品视频免费网站 | 日本黄色一级 | 麻豆爱爱视频 | 精品少妇人妻一区二区黑料社区 | 欧美一区二区三区爱爱 | 国产娇小hdxxxx乱 | 秋霞国产 | 中文字幕高清在线 | 18无码粉嫩小泬无套在线观看 | 成人免费看片98欧美 | 欧洲亚洲激情 | 老女人一毛片 | 国产a免费 | a黄视频 | 日韩欧美偷拍 | 日本黄页网址 | 少妇无码一区二区三区 | 亚洲春色一区二区三区 | 久久riav| 一级黄色片看看 | 欧美h网站 | 国产91绿帽单男绿奴 | 一区二区成人在线 | 亚洲精品乱码久久久久久日本蜜臀 | 伊人夜夜| 88国产精品视频一区二区三区 | 十八禁视频网站在线观看 | 韩日欧美 | 亚洲女优一区 | 激情福利 | 性色免费视频 | 91高跟黑色丝袜呻吟在线观看 | 黑人巨大精品人妻一区二区 | 欧美性生活一区二区三区 | av不卡网站| a级在线看 | 久久这里只精品 | 中文字幕欲求不满 | 日韩3区 | 中文字幕 亚洲一区 | 青青草原免费观看 | 国产农村av| 玖玖视频在线 | 久久久99精品国产一区二区三区 | 成人欧美一级特黄 | 九九久久九九久久 | 懂色av蜜臂av粉嫩av | 麻豆亚洲av熟女国产一区二 | 国产欧美一区二区精品性色超碰 | 国产免费999 | 国产欧美日韩精品区一区二污污污 | 欧美日韩制服 | 综合五月婷婷 | 亚洲女优视频 | 国产黄色激情视频 | 成人网址在线观看 | 美国美女群体交乱 | 美女福利在线视频 | av天堂一区二区 | 国内精品久久久久久久影视简单 | 丁香婷婷网| 未满十八18禁止免费无码网站 | 午夜电影在线播放 | 色屋视频 | 欧美色图第一页 | 操你妹影院 | 国产精品国产三级国产传播 | 狠狠干香蕉 | 性生交大片免费看3p | 成人性做爰片免费视频 | 色香蕉网站 | 97超碰人人干 | 久久精品屋 | 久久婷婷五月国产色综合激情 | 国产二级一片内射视频播放 | 亚洲欧洲免费视频 | 黄色一级片毛片 | 日韩高清二区 | 五月天婷婷社区 | 九热精品 | 性做久久久久久久 | 亚洲精品久久久中文字幕痴女 | 伊人久久中文 | caobi视频| 午夜影院在线免费观看 | 日本视频中文字幕 | 亚洲一区二区国产精品 | 永久免费无码av网站在线观看 | 亚洲乱熟女一区二区三区小说 | 国产一级网站 | 韩日一区二区三区 | 天天av天天干 | 日韩一区在线观看视频 | 国产免费午夜 |