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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

[C++ STL] map使用详解

發布時間:2025/4/16 c/c++ 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [C++ STL] map使用详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、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 mapmap<int, string>::iterator it = mp.begin();for (it = mp.begin(); it != mp.end(); it++)cout << it->first << " " << it->second << endl;//清空容器內的所有元素mp.clear();//display mapcout << "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); //okmp2.swap(mp1);//通過iterator遍歷mp1map<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 不行。

轉載于:https://www.cnblogs.com/linuxAndMcu/p/10261263.html

總結

以上是生活随笔為你收集整理的[C++ STL] map使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。