C++進階-3-5-set/multiset容器

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

C++進階-3-5-set/multiset容器 1 #include<iostream> 2 #include<set> 3 using namespace std; 4 5 // set/multiset容器 6 7 void printSet(set<int>& s) { 8 9 for (s ...


C++進階-3-5-set/multiset容器

  1 #include<iostream>
  2 #include<set>
  3 using namespace std;
  4 
  5 // set/multiset容器
  6 
  7 void printSet(set<int>& s) {
  8 
  9     for (set<int>::iterator it = s.begin(); it != s.end(); it++)
 10     {
 11         cout << *it << " ";
 12     }
 13     cout << endl;
 14 }
 15 
 16 // 1.構造和賦值
 17 void test01() {
 18 
 19     set<int> s1;
 20 
 21     // 插入數據,只有insert
 22     s1.insert(10);
 23     s1.insert(40);
 24     s1.insert(30);
 25     s1.insert(20);
 26     s1.insert(30);
 27 
 28     // 元素插入,自動排序,且不運行有重覆數據
 29     printSet(s1);
 30 
 31     // 拷貝構造
 32     set<int>s2(s1);
 33     printSet(s2);
 34 
 35     // 賦值
 36     set<int>s3;
 37     s3 = s2;
 38     printSet(s3);
 39 
 40 }
 41 
 42 // 2.大小和交換
 43 void test02() {
 44 
 45     set<int> s1;
 46 
 47     s1.insert(10);
 48     s1.insert(30);
 49     s1.insert(20);
 50     s1.insert(40);
 51 
 52     printSet(s1);
 53 
 54     // 判斷是否為空
 55     if (s1.empty()) {
 56         cout << "s1為空" << endl;
 57     }
 58     else {
 59         cout << "s1不為空" << endl;
 60         cout << "s1的大小為:" << s1.size() << endl;
 61     }
 62 
 63     // 交換
 64     set<int> s2;
 65 
 66     s2.insert(100);
 67     s2.insert(300);
 68     s2.insert(200);
 69     s2.insert(400);
 70 
 71     cout << "交換前:" << endl;
 72     printSet(s1);
 73     printSet(s2);
 74 
 75     cout << "交換後:" << endl;
 76     s1.swap(s2);
 77     printSet(s1);
 78     printSet(s2);
 79 
 80 }
 81 
 82 // 3.插入和刪除
 83 void test03() {
 84 
 85     set<int> s1;
 86 
 87     // 插入
 88     s1.insert(10);
 89     s1.insert(30);
 90     s1.insert(20);
 91     s1.insert(40);
 92 
 93     // 遍歷
 94     printSet(s1);
 95 
 96     // 刪除
 97     s1.erase(s1.begin());
 98     printSet(s1);
 99 
100     // 刪除重載版本
101     s1.erase(30);
102     printSet(s1);
103 
104     // 清空
105     //s1.erase(s1.begin(), s1.end())
106     s1.clear();
107     printSet(s1);
108 
109 }
110 
111 // 4.查找和統計
112 void test04() {
113 
114     set<int> s1;
115 
116     s1.insert(10);
117     s1.insert(30);
118     s1.insert(20);
119     s1.insert(40);
120 
121     printSet(s1);
122 
123     // 查找
124     // 存在,返回該元素的迭代器,不存在,返回set.end();
125 
126     set<int>::iterator pos = s1.find(30);
127     if (pos != s1.end()) {
128         cout << "找到元素" << endl;
129     }
130     else
131     {
132         cout << "未找到元素" << endl;
133     }
134 
135     // 統計
136     int num = s1.count(40);  // 統計40的個數
137     cout << "num = " << num << endl;
138     // 對於set而言,統計結果,要麼是0, 要麼是1
139 
140 }
141 
142 // 5. set和multiset區別
143 void test05() {
144 
145     set<int> s;
146 
147     // set插入數據的同時還會返回插入的結果,表示插入是否成功
148     pair<set<int>::iterator, bool>set = s.insert(10);
149 
150     if (set.second) {
151         cout << "第一次插入成功!" << endl;
152     }
153     else
154     {
155         cout << "第一次插入失敗!" << endl;
156     }
157 
158     set = s.insert(10);
159 
160     if (set.second) {
161         cout << "第二次插入成功!" << endl;
162     }
163     else
164     {
165         cout << "第二次插入失敗!" << endl;
166     }
167 
168 
169     // multiset允許插入重覆值
170     multiset<int>ms;
171     ms.insert(10);
172     ms.insert(10);
173     ms.insert(10);
174 
175     for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) {
176         cout << *it << " ";
177     }
178     cout << endl;
179 }
180 
181 
182 // 6.pair對組創建
183 void test06() {
184 
185     // pair對組,成對出現的數據,利用對組可以返回兩個數據
186 
187     // 第一種方式
188     pair<string, int>p(string("Tom"), 20);
189     cout << "姓名:" << p.first << " 年齡:" << p.second << endl;
190 
191     // 第二種數據
192     pair<string, int>p2 = make_pair("Jerry", 30);
193     cout << "姓名:" << p2.first << " 年齡:" << p2.second << endl;
194 
195 }
196 
197 // 7.set容器排序
198 
199 class MyCompare {
200 public:
201     // 仿函數,重寫(),類似於函數
202     bool operator()(int v1, int v2) {
203         return v1 > v2;
204     }
205 };
206 
207 void test07() {
208 
209     // set排序是由小到大的升序
210     // 利用仿函數,可以改變排序規則
211 
212     set<int> s1;
213 
214     s1.insert(10);
215     s1.insert(30);
216     s1.insert(20);
217     s1.insert(40);
218 
219     printSet(s1);
220 
221     // 改為從大到小,在插入之前操作
222 
223     set<int, MyCompare> s2;
224 
225     s2.insert(10);
226     s2.insert(30);
227     s2.insert(20);
228     s2.insert(40);
229 
230     for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
231     {
232         cout << *it << " ";
233     }
234     cout << endl;
235 }
236 
237 
238 int main() {
239 
240     // 1.構造和賦值
241     //test01();
242 
243     // 2.大小和交換
244     //test02();
245 
246     // 3.插入和刪除
247     //test03();
248 
249     // 4.查找和統計
250     //test04();
251 
252     // 5. set和multiset區別
253     //test05();
254     
255     // 6.pair對組創建
256     //test06();
257 
258     // 7.set容器排序--內置數據類型
259     //test07();
260 
261     system("pause");
262 
263     return 0;
264 }
265 
266 // 總結
267 // 
268 // set/multiset容器
269 // 
270 // 簡介:所有元素都會在插入時自動排序
271 // 
272 // 本質:set/multiset屬於關聯式容器,底層結構是二叉樹實現
273 // 
274 // set/multiset區別:
275 //    1.set不允許容器中有重覆的元素
276 //    2.multiset允許容器中有重覆的元素
277 //    3.set插入數據的同時還會返回插入的結果,表示插入是否成功
278 //    4.multiset不會檢測數據,因此可以插入重覆數據
279 // 

 


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

-Advertisement-
Play Games
更多相關文章
  • 一、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用於調試,並分析了優點與不足 ...
  • 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 ...
  • 模塊與包 一、Python 模塊簡介 在開發過程中,隨著程式代碼越寫越多,在一個文件里代碼就會越來越長,越來越不容易維護。 後面我們學習了函數,知道函數是實現一項或多項功能的一段程式,這樣就更方便我們重覆使用代碼。 緊接著,我們有學了類,類可以封裝方法和變數(屬性)。這樣就更方便我們維護代碼了。 我 ...
  • 爬取圖片實例 •selenium+win32爬取圖片 Python學習交流Q群:903971231##### """爬取圖片""" import os import threading import time from ctypes import windll import requests imp ...
一周排行
    -Advertisement-
    Play Games
  • 1、預覽地址:http://139.155.137.144:9012 2、qq群:801913255 一、前言 隨著網路的發展,企業對於信息系統數據的保密工作愈發重視,不同身份、角色對於數據的訪問許可權都應該大相徑庭。 列如 1、不同登錄人員對一個數據列表的可見度是不一樣的,如數據列、數據行、數據按鈕 ...
  • 前言 上一篇文章寫瞭如何使用RabbitMQ做個簡單的發送郵件項目,然後評論也是比較多,也是準備去學習一下如何確保RabbitMQ的消息可靠性,但是由於時間原因,先來說說設計模式中的簡單工廠模式吧! 在瞭解簡單工廠模式之前,我們要知道C#是一款面向對象的高級程式語言。它有3大特性,封裝、繼承、多態。 ...
  • Nodify學習 一:介紹與使用 - 可樂_加冰 - 博客園 (cnblogs.com) Nodify學習 二:添加節點 - 可樂_加冰 - 博客園 (cnblogs.com) 介紹 Nodify是一個WPF基於節點的編輯器控制項,其中包含一系列節點、連接和連接器組件,旨在簡化構建基於節點的工具的過程 ...
  • 創建一個webapi項目做測試使用。 創建新控制器,搭建一個基礎框架,包括獲取當天日期、wiki的請求地址等 創建一個Http請求幫助類以及方法,用於獲取指定URL的信息 使用http請求訪問指定url,先運行一下,看看返回的內容。內容如圖右邊所示,實際上是一個Json數據。我們主要解析 大事記 部 ...
  • 最近在不少自媒體上看到有關.NET與C#的資訊與評價,感覺大家對.NET與C#還是不太瞭解,尤其是對2016年6月發佈的跨平臺.NET Core 1.0,更是知之甚少。在考慮一番之後,還是決定寫點東西總結一下,也回顧一下.NET的發展歷史。 首先,你沒看錯,.NET是跨平臺的,可以在Windows、 ...
  • Nodify學習 一:介紹與使用 - 可樂_加冰 - 博客園 (cnblogs.com) Nodify學習 二:添加節點 - 可樂_加冰 - 博客園 (cnblogs.com) 添加節點(nodes) 通過上一篇我們已經創建好了編輯器實例現在我們為編輯器添加一個節點 添加model和viewmode ...
  • 前言 資料庫併發,數據審計和軟刪除一直是數據持久化方面的經典問題。早些時候,這些工作需要手寫複雜的SQL或者通過存儲過程和觸發器實現。手寫複雜SQL對軟體可維護性構成了相當大的挑戰,隨著SQL字數的變多,用到的嵌套和複雜語法增加,可讀性和可維護性的難度是幾何級暴漲。因此如何在實現功能的同時控制這些S ...
  • 類型檢查和轉換:當你需要檢查對象是否為特定類型,並且希望在同一時間內將其轉換為那個類型時,模式匹配提供了一種更簡潔的方式來完成這一任務,避免了使用傳統的as和is操作符後還需要進行額外的null檢查。 複雜條件邏輯:在處理複雜的條件邏輯時,特別是涉及到多個條件和類型的情況下,使用模式匹配可以使代碼更 ...
  • 在日常開發中,我們經常需要和文件打交道,特別是桌面開發,有時候就會需要載入大批量的文件,而且可能還會存在部分文件缺失的情況,那麼如何才能快速的判斷文件是否存在呢?如果處理不當的,且文件數量比較多的時候,可能會造成卡頓等情況,進而影響程式的使用體驗。今天就以一個簡單的小例子,簡述兩種不同的判斷文件是否... ...
  • 前言 資料庫併發,數據審計和軟刪除一直是數據持久化方面的經典問題。早些時候,這些工作需要手寫複雜的SQL或者通過存儲過程和觸發器實現。手寫複雜SQL對軟體可維護性構成了相當大的挑戰,隨著SQL字數的變多,用到的嵌套和複雜語法增加,可讀性和可維護性的難度是幾何級暴漲。因此如何在實現功能的同時控制這些S ...