大家好!我是【AI 菌】,一枚愛彈吉他的程式員,我
熱愛AI、熱愛分享、熱愛開源! 這博客是我對學習的一點總結與思考,如果您也對深度學習、機器視覺、演算法、Python、C++感興趣,可以關注我的動態,我們一起學習,一起進步~
我的博客地址為:【AI 菌】的博客
我的Github專案地址是:【AI 菌】的Github
目錄
- 一、STL map 類簡介
- 二、map 和 multimap 類的基本操作
- 2.1 實體化操作
- 2.2 兩種排序方式
- 三、map 和 multimap 類的成員函式
- 2.1 插入元素函式 insert()
- 2.2 查找元素函式find()
- 2.3 洗掉元素函式erase()
- 養成習慣,先贊后看!你的支持是我創作的最大動力!
一、STL map 類簡介
map 和 multimap 是鍵-值對容器,支持根據鍵進行查找,和資料結構中的字典類相似,
map 和 multimup 都是模板類,它們之間的區別在于,multimap 能夠存盤重復的鍵,而前者只能存盤唯一的鍵,
為了實作快速查找,map 和 multimap 的內部結構看起來像棵二叉樹,這意味著在 map 和 multimap 中插入元素時將進行排序,還意味著不能像vector那樣可以使用其他元素替換給定位置的元素,位于map中特定位置的元素不能替換為值不同的新元素,這是因為map將把新元素同二叉樹中的其他元素進行比較,進而將它放在其他的位置,
另外,需要注意的是,要使用STL map和 multimap類時,需要包含頭檔案:
# include<map>

二、map 和 multimap 類的基本操作
2.1 實體化操作
典型的 map 實體化語法如下:
map <keyType, valueType, Predicate=std::less <keyType> > mapObject;
multimap <keyType, valueType, Predicate=std::greater <keyType> >mmapObject;
- 第一個引數keyType,代表的是鍵的型別
- 第二個引數valueType,代表的是值的型別
- 第三個引數less/greater,用作排序標準,less是升序排序,greater是降序排序,
下面使用了幾種不同的方法,實體化了鍵值對{string, int}型別的 map 或 multimap,
#include <iostream>
#include <map>
#include <string>
using namespace std;
template<typename KeyType>
int main()
{
// 方法1
map<int, string> map1;
multimap<int, string> mmap1;
// 方法2
map<int, string> map2(map1);
multimap<int, string> mmap2(mmap1);
// 方法3
map<int, string> map3(map1.cbegin(), map1.cend());
multimap<int, string> mmap3(mmap1.cbegin(), mmap1.cend());
return 0;
}
2.2 兩種排序方式
在實體化 map 和 multimap 程序中,如果沒有指定排序的詞謂,那么 map 會默認對其內的元素進行升序排序,
想要自定義map的排序標準,我們可以通過以下兩種方法:
(1)使用map類默認提供的詞謂 less 和 greater 來確定排序標準
// 1.升序
map<int, string, less<int>> map1;
// 2.降序
multimap<int, string, greater<int>> mmap1;
(2)自定義詞謂用作排序標準
#include <iostream>
#include <map>
#include <string>
using namespace std;
template<typename KeyType>
// 自定義詞謂
struct ReverseSort
{
bool operator()(const KeyType& key1, const KeyType& key2)
{
return (key1 > key2);
}
};
int main()
{
// 實體化
map<int, string> map1;
multimap<int, string> mmap1;
// 降序排列
map<int, string, ReverseSort<int>> map5(map1.cbegin(), map1.cend());
multimap<int, string, ReverseSort<int>> mmap5(mmap1.cbegin(), mmap1.cend());
return 0;
}
三、map 和 multimap 類的成員函式
2.1 插入元素函式 insert()
使用map的成員函式insert(),可以向map中插入新的鍵值對;下面演示了4種常用的插入方式:
#include <iostream>
#include <map>
#include <string>
using namespace std;
template <typename T>
// 顯示內容
void DisplayContents(const T& Input)
{
for(auto iElement = Input.cbegin(); iElement != Input.cend(); ++iElement)
cout<<iElement->first<<" "<<iElement->second<<endl;
cout<<endl;
}
int main()
{
map<int, string, less<int>>map1;
// 插入方法1
map1.insert(map<int, string>::value_type(3, "Three"));
// 插入方法2
map1.insert(make_pair(1, "One"));
// 插入方法3
map1.insert(pair<int, string>(10, "Ten"));
// 插入方法4,采用類似于陣列的語法進行插入,
map1[8] = "Eight";
cout<<"鍵 :值"<<endl;
DisplayContents(map1);
return 0;
}
進行類四次插入操作后,map里有四個鍵值對,顯示如下:

2.2 查找元素函式find()
map 和 multimap 提供了成員函式find(),它能夠根據給定的鍵查找值,
find()回傳的是一個迭代器,如下所示:
map1 <int, string>::const_iterator iFound = map1.find(Key);
使用find()進行查找元素時,要先檢查迭代器,確保find()已成功,再使用它來訪問找到的值:
if(iFound != map1.end())
{
cout<<"鍵 :值"<<endl;
cout<<iFound->first<<" "<<iFound->second<<endl;
}
else
cout<<"Key不存在于map中!"<<endl;
下面演示一個實際的例子, 使用成員函式find()查找map1中鍵所對應的值:
#include <iostream>
#include <map>
#include <string>
using namespace std;
template <typename T>
// 顯示內容
void DisplayContents(const T& Input)
{
for(auto iElement = Input.cbegin(); iElement != Input.cend(); ++iElement)
cout<<iElement->first<<" "<<iElement->second<<endl;
cout<<endl;
}
int main()
{
map<int, string, less<int>>map1;
// 插入元素
map1.insert(make_pair(8, "Eight"));
map1.insert(make_pair(1, "One"));
map1.insert(make_pair(6, "Six"));
map1.insert(make_pair(10, "Ten"));
// 顯示鍵值
cout<<"鍵 :值"<<endl;
DisplayContents(map1);
// 根據鍵查找其對應的值
auto iFound = map1.find(8);
if(iFound != map1.end())
cout<<iFound->first<<"對應的值是:"<<iFound->second<<endl;
else
cout<<"Key不存在于map1中!"<<endl;
return 0;
}
運行結果如下:

如果使用的是multimap,容器可能包含多個鍵相同的鍵值對,因此需要找到與指定鍵對應的所有值,所以,可先使用count()確定有多少個值與指定的鍵對應,再對迭代器遞增,以訪問所有對應的值,
下面的map存在3個相同的鍵值對,線要找到該鍵值對,并依次輸出他們:
#include <iostream>
#include <map>
#include <string>
using namespace std;
template <typename T>
// 顯示內容
void DisplayContents(const T& Input)
{
for(auto iElement = Input.cbegin(); iElement != Input.cend(); ++iElement)
cout<<iElement->first<<" "<<iElement->second<<endl;
cout<<endl;
}
int main()
{
multimap<int, string, less<int>>map1;
// 插入元素
map1.insert(make_pair(8, "Eight"));
map1.insert(make_pair(1, "One"));
map1.insert(make_pair(8, "Eight"));
map1.insert(make_pair(8, "Eight"));
// 顯示鍵值
cout<<"鍵 :值"<<endl;
DisplayContents(map1);
// 根據鍵查找其所有對應的值
auto iFound = map1.find(8);
if(iFound != map1.end())
{
// 統計multimap中鍵8出現的次數
size_t num = map1.count(8);
for(size_t nCounter=0; nCounter<num; ++nCounter)
{
cout<<"Key: "<<iFound->first<<", Val["<<nCounter<<"]="<<iFound->second<<endl;
++iFound;
}
}
else
cout<<"Element Not Found In The Map!";
return 0;
}
運行結果:

2.3 洗掉元素函式erase()
map 和 multimap 提供了成員函式erase(),該函式用于洗掉容器中的元素,主要有以下三種方法:
- 將鍵作為引數時,這將洗掉包含指定鍵的所有鍵值對
map1.erase(key);
- 以迭代器為引數,洗掉迭代器指向的引數
map1.erase(iFound);
- 使用迭代器指定邊界,洗掉指定范圍內的所有元素
map1.erase(iLowerBound, iUpperBound);
下面實際演示以上三種方法的使用:
#include <iostream>
#include <map>
#include <string>
using namespace std;
template <typename T>
// 顯示內容
void DisplayContents(const T& Input)
{
for(auto iElement = Input.cbegin(); iElement != Input.cend(); ++iElement)
cout<<iElement->first<<" "<<iElement->second<<endl;
cout<<endl;
}
int main()
{
multimap<int, string, less<int>>map1;
// 插入元素
map1.insert(make_pair(8, "Eight"));
map1.insert(make_pair(1, "One"));
map1.insert(make_pair(6, "Six"));
map1.insert(make_pair(10, "Ten"));
map1.insert(make_pair(15, "Fifteen"));
// 顯示鍵值
cout<<"鍵 :值"<<endl;
DisplayContents(map1);
// 洗掉鍵1對應的鍵值對
map1.erase(1);
cout<<"洗掉鍵1對應的鍵值對后:"<<endl;
DisplayContents(map1);
// 洗掉迭代器指向的元素
auto iFound = map1.find(8);
map1.erase(iFound);
cout<<"洗掉鍵8對應的鍵值對后:"<<endl;
DisplayContents(map1);
// 洗掉指定范圍內(6至10)的元素
map1.erase(map1.lower_bound(6), map1.lower_bound(15));
cout<<"洗掉指定范圍內(從6開始,到15之前)的元素:"<<endl;
DisplayContents(map1);
return 0;
}
運行結果:

今天的分享就到這里啦,希望對你的學習有所幫助!

最后,我有幸入選了CSDN原創博客大賽Top50,如果您能為我投出寶貴的一票,將是對我創作的最大鼓勵!投票完后還能參與官方抽獎喲!
投票通道:CSDN原創博客大賽

養成習慣,先贊后看!你的支持是我創作的最大動力!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/112156.html
標籤:其他
下一篇:漫畫:什么是 “黑天鵝事件” ?
