C++中set/multiset容器詳解(附測試用例與結(jié)果圖)
1 set/ multiset 容器
1.1 set基本概念
簡介:
- 所有元素都會在插入時自動被排序
本質(zhì):
- set/multiset屬于關(guān)聯(lián)式容器,底層結(jié)構(gòu)是用二叉樹實現(xiàn)。
set和multiset區(qū)別:
- set不允許容器中有重復(fù)的元素
- multiset允許容器中有重復(fù)的元素
1.2 set構(gòu)造和賦值
功能描述:創(chuàng)建set容器以及賦值
構(gòu)造:
set<T> st;
//默認構(gòu)造函數(shù):set(const set &st);
//拷貝構(gòu)造函數(shù)
賦值:
set& operator=(const set &st);
//重載等號操作符
示例:
#include <set> void printSet(set<int> & s) { for (set<int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl; } //構(gòu)造和賦值 void test01() { set<int> s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); printSet(s1); //拷貝構(gòu)造 set<int>s2(s1); printSet(s2); //賦值 set<int>s3; s3 = s2; printSet(s3); } int main() { test01(); system("pause"); return 0; }
總結(jié):
- set容器插入數(shù)據(jù)時用insert
- set容器插入數(shù)據(jù)的數(shù)據(jù)會自動排序
1.3 set大小和交換
功能描述:
統(tǒng)計set容器大小以及交換set容器
函數(shù)原型:
size();
//返回容器中元素的數(shù)目empty();
//判斷容器是否為空swap(st);
//交換兩個集合容器
示例:
void printSet(set<int> & s) { for (set<int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl; } //大小 void test01() { set<int> s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); if (s1.empty()) { cout << "s1為空" << endl; } else { cout << "s1不為空" << endl; cout << "s1的大小為: " << s1.size() << endl; cout << endl; } } //交換 void test02() { set<int> s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); set<int> s2; s2.insert(100); s2.insert(300); s2.insert(200); s2.insert(400); cout << "交換前" << endl; printSet(s1); printSet(s2); cout << endl; cout << "交換后" << endl; s1.swap(s2); printSet(s1); printSet(s2); } int main() { test01(); test02(); system("pause"); return 0; }
總結(jié):
- 統(tǒng)計大小 — size
- 判斷是否為空 — empty
- 交換容器 — swap
1.4 set插入和刪除
功能描述:
set容器進行插入數(shù)據(jù)和刪除數(shù)據(jù)
函數(shù)原型:
- insert(elem); //在容器中插入元素。
- clear(); //清除所有元素
- erase(pos); //刪除pos迭代器所指的元素,返回下一個元素的迭代器。
- erase(beg, end); //刪除區(qū)間[beg,end)的所有元素 ,返回下一個元素的迭代器。
- erase(elem); //刪除容器中值為elem的元素。
示例:
#include <set> void printSet(set<int> & s) { for (set<int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl; } //插入和刪除 void test01() { set<int> s1; //插入 s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); printSet(s1); //刪除 s1.erase(s1.begin()); printSet(s1); s1.erase(30); printSet(s1); //清空 //s1.erase(s1.begin(), s1.end()); s1.clear(); printSet(s1); } int main() { test01(); system("pause"); return 0; }
總結(jié):
- 插入 — insert
- 刪除 — erase
- 清空 — clear
1.5 set查找和統(tǒng)計
功能描述:
對set容器進行查找數(shù)據(jù)以及統(tǒng)計數(shù)據(jù)
函數(shù)原型:
find(key);
//查找key是否存在,若存在,返回該鍵的元素的迭代器;若不存在,返回set.end();count(key);
//統(tǒng)計key的元素個數(shù)
示例:
#include <set> //查找和統(tǒng)計 void test01() { set<int> s1; //插入 s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); //查找 set<int>::iterator pos = s1.find(30); if (pos != s1.end()) { cout << "找到了元素 : " << *pos << endl; } else { cout << "未找到元素" << endl; } //統(tǒng)計 int num = s1.count(30); cout << "num = " << num << endl; } int main() { test01(); system("pause"); return 0; }
總結(jié):
- 查找 — find (返回的是迭代器)
- 統(tǒng)計 — count (對于set,結(jié)果為0或者1)
1.6 set和multiset區(qū)別
學(xué)習(xí)目標:
掌握set和multiset的區(qū)別
區(qū)別:
- set不可以插入重復(fù)數(shù)據(jù),而multiset可以
- set插入數(shù)據(jù)的同時會返回插入結(jié)果,表示插入是否成功
- multiset不會檢測數(shù)據(jù),因此可以插入重復(fù)數(shù)據(jù)
第7行的pair<set<int>::iterator, bool>
是怎么來的呢?首先將鼠標停留在第七行的insert上,點擊鼠標右鍵,選擇轉(zhuǎn)到定義,會發(fā)現(xiàn)insert函數(shù)的返回類型是_Pairib,如下圖
再將鼠標停留在_Pairib上,點擊鼠標右鍵,選擇轉(zhuǎn)到定義,就會發(fā)現(xiàn)_Pairib是pair,里面有兩個數(shù)據(jù),第一個數(shù)據(jù)是迭代器
示例:
#include <set> //set和multiset區(qū)別 void test01() { set<int> s; pair<set<int>::iterator, bool> ret = s.insert(10); if (ret.second) { cout << "第一次插入成功!" << endl; } else { cout << "第一次插入失敗!" << endl; } ret = s.insert(10); if (ret.second) { cout << "第二次插入成功!" << endl; } else { cout << "第二次插入失敗!" << endl; } //multiset multiset<int> ms; ms.insert(10); ms.insert(10); for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) { cout << *it << " "; } cout << endl; } int main() { test01(); system("pause"); return 0; }
總結(jié):
- 如果不允許插入重復(fù)數(shù)據(jù)可以利用set
- 如果需要插入重復(fù)數(shù)據(jù)利用multiset
1.7 set容器排序
學(xué)習(xí)目標:
- set容器默認排序規(guī)則為從小到大,掌握如何改變排序規(guī)則
主要技術(shù)點:
- 利用
仿函數(shù)
(重載了運算符或小括號),可以改變排序規(guī)則
示例一: set存放內(nèi)置數(shù)據(jù)類型
如果你想要set中的元素按自己想要的順序進行排序,那么就要在沒有插入元素前告訴set排序規(guī)則
#include <set> class MyCompare //不一定非要是MyCompare,隨便命名 { public: //第一個()代表重載小括號,第二個()代表函數(shù)的參數(shù)列表 bool operator()(int v1, int v2) { //降序,也就是想要第一個數(shù)大于第二個數(shù) return v1 > v2; } }; void test01() { set<int> s1; s1.insert(10); s1.insert(40); s1.insert(20); s1.insert(30); s1.insert(50); //默認從小到大 for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) { cout << *it << " "; } cout << endl; //指定排序規(guī)則,<>肯定是放數(shù)據(jù)類型,不能放函數(shù)名,而仿函數(shù)Mycompare本質(zhì)上就是一個類型 //插入數(shù)之后就沒辦法排序了,插入之前就要指定排序規(guī)則 set<int,MyCompare> s2; s2.insert(10); s2.insert(40); s2.insert(20); s2.insert(30); s2.insert(50); for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) { cout << *it << " "; } cout << endl; } int main() { test01(); system("pause"); return 0; }
示例二: set存放自定義數(shù)據(jù)類型
#include <set> #include <string> class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age; }; class comparePerson { public: bool operator()(const Person& p1, const Person &p2) { //按照年齡進行排序 降序 return p1.m_Age > p2.m_Age; } }; void test01() { set<Person, comparePerson> s; Person p1("劉備", 23); Person p2("關(guān)羽", 27); Person p3("張飛", 25); Person p4("趙云", 21); s.insert(p1); s.insert(p2); s.insert(p3); s.insert(p4); for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++) { cout << "姓名: " << it->m_Name << " 年齡: " << it->m_Age << endl; } } int main() { test01(); system("pause"); return 0; }
總結(jié)
到此這篇關(guān)于C++中set/multiset容器詳解的文章就介紹到這了,更多相關(guān)C++ set/multiset容器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用c++實現(xiàn)OpenCV繪制旋轉(zhuǎn)矩形圖形
這篇文章主要給大家介紹了使用c++實現(xiàn)OpenCV繪制圖形旋轉(zhuǎn)矩形的方法案例,通過圖文及代碼形式進行了詳細的描述,有需要的朋友可以參考下,希望可以有所幫助2021-08-08Win11+?VS2022編譯?FFmpeg6.0?靜態(tài)庫的詳細過程
這篇文章主要介紹了Win11+VS2022編譯FFmpeg6.0靜態(tài)庫的方法,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08VC++ 字符串String MD5計算小工具 VS2008工程
基于字符串加密的MD5算法,VS2008 VC++,多字節(jié)編譯工程。主要代碼如下,實現(xiàn)了ANSI字符串加密與Unicode字符串加密,需要的朋友可以參考下2017-07-07