C++進階-3-6-map/multimap容器

来源:https://www.cnblogs.com/LYH-win/archive/2022/05/08/16245698.html
-Advertisement-
Play Games

C++進階-3-6-map/multimap容器 1 #include<iostream> 2 #include<map> 3 using namespace std; 4 5 // map / multimap容器 6 7 void printMap(map<int, int>& m) { 8 f ...


C++進階-3-6-map/multimap容器

  1 #include<iostream>
  2 #include<map>
  3 using namespace std;
  4 
  5 // map / multimap容器
  6 
  7 void printMap(map<int, int>& m) {
  8     for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
  9         cout << "key = " << (*it).first << " value = " << it->second << endl;
 10     }
 11     cout << endl;
 12 }
 13 
 14 // 1.構造和賦值
 15 void test01() {
 16 
 17     // 創建map容器
 18     map<int, int> m;
 19 
 20     m.insert(pair<int, int>(1, 10));
 21     m.insert(pair<int, int>(2, 20));
 22     m.insert(pair<int, int>(3, 30));
 23     m.insert(pair<int, int>(4, 40));
 24 
 25     printMap(m);
 26 
 27     // 拷貝構造
 28     map<int, int>m2(m);
 29     printMap(m2);
 30 
 31     // 賦值
 32     map<int, int>m3;
 33     m3 = m2;
 34     printMap(m3);
 35 
 36 }
 37 
 38 // 2.大小和交換
 39 void test02() {
 40 
 41     map<int, int> m;
 42 
 43     m.insert(pair<int, int>(1, 10));
 44     m.insert(pair<int, int>(2, 20));
 45     m.insert(pair<int, int>(3, 30));
 46     m.insert(pair<int, int>(4, 40));
 47 
 48     //printMap(m);
 49 
 50     // 大小
 51     if (m.empty()) {
 52         cout << "m 為空" << endl;
 53     }
 54     else
 55     {
 56         cout << "m 不為空" << endl;
 57         cout << "m 的大小為:" << m.size() << endl;
 58     }
 59 
 60     // 交換
 61     map<int, int> m2;
 62 
 63     m2.insert(pair<int, int>(5, 50));
 64     m2.insert(pair<int, int>(6, 60));
 65     m2.insert(pair<int, int>(7, 70));
 66     m2.insert(pair<int, int>(8, 80));
 67 
 68     cout << "交換前:" << endl;
 69     printMap(m);
 70     printMap(m2);
 71 
 72     cout << "交換後:" << endl;
 73     m.swap(m2);
 74     printMap(m);
 75     printMap(m2);
 76 }
 77 
 78 // 3.插入和刪除
 79 void test03() {
 80 
 81     map<int, int> m;
 82 
 83     // 插入
 84     // 第一種
 85     m.insert(pair<int, int>(1, 10));
 86     printMap(m);
 87 
 88     // 第二種
 89     m.insert(make_pair(2, 20));
 90     printMap(m);
 91 
 92     // 第三種
 93     m.insert(map<int, int>::value_type(3, 30));
 94     printMap(m);
 95 
 96     // 第四種
 97     m[4] = 40;  // 不建議插入使用
 98     printMap(m);
 99 
100 
101     // 刪除
102     m.erase(m.begin());
103     printMap(m);
104 
105     m.erase(3);  // 按照key刪除
106     printMap(m);
107 
108     // 清空
109     //m.erase(m.begin(), m.end());
110     m.clear();
111     printMap(m);
112 }
113 
114 // 4.查找和統計
115 void test04() {
116 
117     map<int, int> m;
118 
119     m.insert(pair<int, int>(1, 10));
120     m.insert(pair<int, int>(2, 20));
121     m.insert(pair<int, int>(3, 30));
122     m.insert(pair<int, int>(4, 40));
123 
124     printMap(m);
125 
126     // 查找,find返回的是迭代器
127     map<int, int>::iterator pos = m.find(3);
128 
129     if (pos != m.end()) {
130         cout << "查到了元素,key = " << (*pos).first << " value = " << pos->second << endl;
131     }
132     else
133     {
134         cout << "未找到元素" << endl;
135     }
136 
137     // 統計, map中無重覆的key,所以,統計值為0或1
138     // multimap的count統計可能大於1
139     int num = m.count(1);
140     cout << "num = " << num << endl;
141 
142 }
143 
144 // 5. 排序
145 
146 class MyCompare {
147 public:
148     bool operator()(int v1, int v2) {
149         // 降序
150         return v1 > v2;
151     }
152 
153 };
154 
155 void printMap1(map<int, int, MyCompare>& m) {
156     for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
157         cout << "key = " << (*it).first << " value = " << it->second << endl;
158     }
159     cout << endl;
160 }
161 
162 void test05() {
163 
164     map<int, int> m;
165 
166     m.insert(pair<int, int>(1, 10));
167     m.insert(pair<int, int>(2, 20));
168     m.insert(pair<int, int>(3, 30));
169     m.insert(pair<int, int>(4, 40));
170 
171     // 排序 預設:從小到大,升序
172     printMap(m);
173 
174     // 降序
175     map<int, int, MyCompare> m2;
176 
177     m2.insert(pair<int, int>(1, 10));
178     m2.insert(pair<int, int>(2, 20));
179     m2.insert(pair<int, int>(3, 30));
180     m2.insert(pair<int, int>(4, 40));
181     
182     printMap1(m2);
183 
184 }
185 
186 int main() {
187 
188     // 1.構造和賦值
189     //test01();
190 
191     // 2.大小和交換
192     //test02();
193 
194     // 3.插入和刪除
195     //test03();
196 
197     // 4.查找和統計
198     //test04();
199 
200     // 5. 排序,預設,從小到大升序
201     test05();
202 
203     system("pause");
204 
205     return 0;
206 }
207 
208 // 總結
209 // 
210 // map / multimap容器
211 // 
212 // 簡介:
213 //    map中所有元素都是pair
214 //    pair中第一個元素為key,起索引作用,第二個元素為value
215 //    所有元素都回根據元素的鍵值自動排序
216 // 
217 // 本質:屬於關聯式容器,地層結構用二叉樹實現
218 // 
219 // 優點:可以根據key值快速找到value值
220 // 
221 // map / multimap區別:
222 //    map不允許容器中有重覆的key
223 //    multi允許容器中有重覆的key
224 //  

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ${}字元串可以用於字元串拼接,一般用於模糊查詢中(因為有sql註入的風險,很少用) ...
  • 在開發過程中,不知道有沒有這樣的經歷,項目實際讀取的配置信息有時候總是與預期不符,今天就來研究下 SpringBoot 讀取配置文件順序。 一、SpringBoot 配置文件載入優先順序 SpringBoot官方文檔說明瞭載入的順序如下,越靠前優先順序越高。 Spring Boot uses a ver ...
  • 前言 嗨嘍!大家好呀 第三方模塊: requests >>> pip install requests 模塊安裝問題: 如果安裝python第三方模塊: win + R 輸入 cmd 點擊確定, 輸入安裝命令 pip install 模塊名 (pip install requests) 回車 在py ...
  • 一、Matplotlib 博文來源:https://www.runoob.com/matplotlib/matplotlib-tutorial.html Matplotlib 是 Python 的繪圖庫,它能讓使用者很輕鬆地將數據圖形化,並且提供多樣化的輸出格式。 Matplotlib 可以用來繪製 ...
  • 在系統開發的過程中,必然存在耗時極高的動作,是基於請求響應模式無法解決的問題,通常會採用解耦的思維,並基於非同步或者事件驅動的方式去調度整個流程的完整執行。 ...
  • 一個工作了2年的粉絲,私信了一個比較簡單的問題。 說: “Spring中事務的傳播行為有哪些?” 他說他能記得一些,但是在項目中基本上不需要配置,所以一下就忘記了。 結果導致面試被拒絕,有點遺憾! ok,關於這個問題,看看普通人和高手的回答。 普通人: 嗯。。。。。。。。 高手: 對於這個問題,需要 ...
  • 函數的定義和使用 def test(x): # x代表形參 ''' 2*x+1 :param x:整形數字 :return:返回計算結果 ''' y = 2*x+1 return y p = test(3) # test()表示運行名為test函數,3代表實參,給x進行賦值 print(p) 函數的 ...
  • 介紹瞭如何在程式代碼中嵌入IPython用於調試,並分析了優點與不足 ...
一周排行
    -Advertisement-
    Play Games
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...