[C++ STL] map使用詳解
阿新 • • 發佈:2019-01-12
一、set介紹:
Map由紅黑樹實現,其元素都是“鍵值/實值”所形成的一個對組(key/value pairs)。每個元素有一個鍵,是排序準則的基礎。每一個鍵只能出現一次,不允許重複。
Map主要用於資料一對一對映(one-to-one)的情況,map內部自建一顆紅黑樹(平衡二叉樹中的一種),這顆樹具有對資料自動排序的功能,所以在map內部所有的資料都是有序的。比如一個班級中,每個學生的學號跟他的姓名就存在著一對一對映的關係。
二、用法
1、標頭檔案
#include <map>
//map屬於std命名域的,因此需要通過命名限定,例如using std::map;
2、定義及初始化
map<int, string> a; //定義一個int型別的對映a
//map<int, string> a(10); //error,未定義這種建構函式
//map<int, string> a(10, 1); //error,未定義這種建構函式
map<int, string> b(a); //定義並用對映a初始化對映b
//map<int, string> b(a.begin(), a.end()); //error,未定義這種建構函式
3、基本操作
(1) 容量函式
- 容器大小: mp.size();
- 容器最大容量: mp.max_size();
更改容器大小: mp.resize();- 容器判空: mp.empty();
- 查詢鍵key的元素個數: mp.count(key);
#include "stdafx.h" #include <iostream> #include <map> #include <string> using namespace std; int main(int argc, char* argv[]) { map<int,string> mp; mp.insert({ 1, "張三" }); mp.insert({ 2, "李四" }); mp.insert(pair<int, string>{ 3, "隔壁老王" }); cout << "元素大小: " << mp.size() << endl; cout << "元素最大容量: " << mp.max_size() << endl; cout << "鍵2的元素個數: " << mp.count(2) << endl; if (mp.empty()) cout << "元素為空" << endl; return 0; } /* 元素大小: 3 元素最大容量: 89478485 鍵2的元素個數: 1 */
(2) 增加函式
- 在容器中插入元素:mp.insert(const T& x);
- 任意位置插入一個元素: mp.insert(iterator it, const T& x);
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
map<int,string> mp;
//在容器中插入元素
mp.insert({ 1, "張三" });
mp.insert({ 2, "李四" });
//任意位置插入一個元素
map<int, string>::iterator it = mp.begin();
mp.insert(it, pair<int, string>{ 3, "隔壁老王" }); //會自動排序
for (it = mp.begin(); it != mp.end(); it++)
cout << it->first << " " << it->second << endl;
cout << endl;
return 0;
}
/*
1 張三
2 李四
3 隔壁老王
*/
(3) 刪除函式
- 刪除鍵值為keyValue的元素: mp.pop_back(const T& keyValue);
- 刪除迭代器所指的元素: mp.erase(iterator it);
- 刪除區間[first,last]之間的所有元素: mp.erase(iterator first, iterator last);
- 清空所有元素: mp.clear();
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
map<int,string> mp;
//在容器中插入元素
mp.insert({ 1, "張三" });
mp.insert({ 2, "李四" });
mp.insert({ 4, "王五" });
mp.insert({ 5, "小明" });
//任意位置插入一個元素
mp.insert(mp.begin(), pair<int, string>{ 3, "隔壁老王" }); //會自動排序
//刪除鍵值為keyValue的元素
mp.erase(2);
//刪除迭代器所指的元素
mp.erase(mp.begin());
//刪除區間[first,last]之間的所有元素
mp.erase(mp.begin(), ++mp.begin());
//display map
map<int, string>::iterator it = mp.begin();
for (it = mp.begin(); it != mp.end(); it++)
cout << it->first << " " << it->second << endl;
//清空容器內的所有元素
mp.clear();
//display map
cout << "mp:";
for (it = mp.begin(); it != mp.end(); it++)
cout << it->first << " " << it->second << endl;
cout << endl;
return 0;
}
/*
4 王五
5 小明
mp:
*/
(4) 迭代器
- 開始迭代器指標:mp.begin();
- 末尾迭代器指標:mp.end(); //指向最後一個元素的下一個位置
- 指向常量的開始迭代器指標: mp.cbegin(); //意思就是不能通過這個指標來修改所指的內容,但還是可以通過其他方式修改的,而且指標也是可以移動的。
- 指向常量的末尾迭代器指標: mp.cend();
- 反向迭代器指標,指向最後一個元素: mp.rbegin();
- 反向迭代器指標,指向第一個元素的前一個元素: mp.rend();
- 返回最後一個key<=keyElem元素的迭代器: mp.lower_bound(keyElem);
- 返回第一個key>keyElem元素的迭代器: mp.upper_bound(keyElem);
- 返回容器中key與keyElem相等的上下限的兩個迭代器,這兩個迭代器被放在對組(pair)中: mp.equal_range(keyElem);
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
map<int,string> mp;
//在容器中插入元素
mp[1] = "張三";
mp[2] = "李四";
mp[3] = "隔壁老王";
cout << "*(mp.begin()): " << mp.begin()->first << endl;
cout << "*(mp.end()): " << (--mp.end())->first << endl;
cout << "*(mp.cbegin()): " << mp.cbegin()->first << endl;
cout << "*(mp.cend()): " << (--mp.cend())->first << endl;
cout << "*(mp.rbegin()): " << mp.rbegin()->first << endl;
cout << "*(mp.rend()): " << (--mp.rend())->first << endl;
cout << "*(mp.lower_bound(2)): " << mp.lower_bound(2)->first << endl;
cout << "*(mp.upper_bound(2)): " << mp.upper_bound(2)->first << endl;
pair<map<int, string>::iterator, map<int, string>::iterator> t_pair = mp.equal_range(2);
cout << "*(t_pair.first): " << t_pair.first->first << endl;
cout << "*(t_pair.second): " << t_pair.second->first << endl;
cout << endl;
return 0;
}
/*
*(mp.begin()): 1
*(mp.end()): 3
*(mp.cbegin()): 1
*(mp.cend()): 3
*(mp.rbegin()): 3
*(mp.rend()): 1
*(mp.lower_bound(2)): 2
*(mp.upper_bound(2)): 3
*(t_pair.first): 2
*(t_pair.second): 3
*/
(5) 訪問函式
- 查詢鍵key是否存在,若存在,返回該鍵的元素的迭代器;若不存在,返回map.end(): mp.find(key);
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
map<int,string> mp;
//在容器中插入元素
mp[1] = "張三";
mp[2] = "李四";
mp[3] = "隔壁老王";
//通過find(key)查詢鍵值
cout << "find(key)查詢鍵: " << mp.find(1)->first << endl;
cout << "find(key)查詢值: " << mp.find(2)->second << endl;
return 0;
}
/*
find(key)查詢鍵: 1
find(key)查詢值: 李四
*/
(6) 其他函式
- 交換兩個同類型容器的元素: swap(map&, map&); 或 mp.swap(map&);
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
map<int,string> mp1;
//在容器中插入元素
mp1[1] = "張三";
mp1[2] = "李四";
mp1[3] = "隔壁老王";
map<int, string> mp2;
//在容器中插入元素
mp2[1] = "tom";
mp2[2] = "jerry";
mp2[3] = "mariy";
//交換兩個容器的元素
//swap(mp1,mp2); //ok
mp2.swap(mp1);
//通過iterator遍歷mp1
map<int, string>::iterator it;
for (it = mp1.begin(); it != mp1.end(); it++)
cout << it->second << " ";
cout << endl;
return 0;
}
/*
tom jerry mariy
*/
(7) 演算法
- 遍歷元素
map<int>::iterator it;
for (it = mp.begin(); it != mp.end(); it++)
cout << it->second << endl;
三、總結
可以看到,map 與set的用法基本一致,只有以下幾處不同:
- map 可以像陣列那樣插入元素,而 set 不行。