C++ STL容器值set

/*
3.7.2 set常用API
3.7.2.1 set構造函數
set<T> st;//set默認構造函數:
mulitset<T> mst; //multiset默認構造函數: 
set(const set& st);//拷貝構造函數
3.7.2.2 set賦值操作
set & operator=(const set & st);//重載等號操作符
swap(st);//交換兩個集合容器
3.7.2.3 set大小操作
size();//返回容器中元素的數目
empty();//判斷容器是否為空3.7.2.4 set插入和刪除操作
insert(elem);//在容器中插入元素。
clear();//清除所有元素
erase(pos);//刪除pos迭代器所指的元素,返回下一個元素的迭代器。
erase(beg, end);//刪除區間[beg,end)的所有元素 ,返回下一個元素的迭代器。
erase(elem);//刪除容器中值為elem的元素。
*/
#include <iostream>
#include <string>
#include <set>
using namespace std;void printSetInt(set<int> &s) {for (set<int>::iterator it = s.begin(); it != s.end(); ++it) {cout << (*it) << endl;}
}
void test1() {//set 是關聯式容器, 關聯式容器在插入的時候自動排好序// 序列式容器, 怎么插入的就怎么排序set<int> s1;s1.insert(5);s1.insert(3);s1.insert(1);s1.insert(7);s1.insert(9);printSetInt(s1);if (s1.empty()) {cout << "s1 is empty" << endl;}else {cout << "s1 size of " << s1.size() << endl;}s1.erase(s1.begin());s1.erase(3);printSetInt(s1);
}
/*
3.7.2.5 set查找操作
find(key);//查找鍵key是否存在,若存在,返回該鍵的元素的迭代器;若不存在,返回set.end();
count(key);//查找鍵key的元素個數
lower_bound(keyElem);//返回第一個key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一個key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key與keyElem相等的上下限的兩個迭代器。
*/
void test2() {set<int> s1;s1.insert(5);s1.insert(3);s1.insert(1);s1.insert(7);s1.insert(9);set<int>::iterator pos = s1.find(3);if (pos!=s1.end()) {cout << "找到了 值為" << (*pos) << endl;}else {cout << "未找到" << endl;}int num = s1.count(1); cout << "1 的個數為" << num << endl;set<int>::iterator lb = s1.lower_bound(3);if (lb != s1.end()) {cout << "lower_bound(3) 為" << *lb << endl;} else {cout << "未找到" << endl;}set<int>::iterator ub = s1.upper_bound(3);if (ub != s1.end()) {cout << "upper_bound(3) 為" << *ub << endl;}else {cout << "未找到" << endl;}pair<set<int>::iterator, set<int>::iterator> er = s1.equal_range(3);if (er.first != s1.end()) {cout << "找到 s1.equal_range(3)的lower_bound為" << *(er.first) << endl;} else {cout << "未找到" << endl;}if (er.second != s1.end()) {cout << "找到 s1.equal_range(3)的upper_bound為" << *(er.second) << endl;}else {cout << "未找到" << endl;}
}// 創建pair的方式   對組
void test3() {//第一種pair<string, int> p(string("Tom"), 10);// 取值cout << "name is " << p.first << endl;cout << "age is " << p.second << endl;//第二種pair<string, int> p2 = make_pair("Jerry", 15);// 取值cout << "name is " << p2.first << endl;cout << "age is " << p2.second << endl;
}void test4() {// set容器不允許插入重復的值set<int> s1;pair<set<int>::iterator, bool> ret = s1.insert(10);if (ret.second) {cout << "10 插入成功" << endl;} else {cout << "10 插入失敗" << endl;}pair<set<int>::iterator, bool> ret2 = s1.insert(10);if (ret2.second) {cout << "10 插入成功" << endl;}else {cout << "10 插入失敗" << endl;}
}//仿函數
class MySort {
public:bool operator()(int v1, int v2) const {return v1 > v2;}
};void printSetIntSort(set<int, MySort> &s) {for (set<int>::iterator it = s.begin(); it != s.end(); ++it) {cout << (*it) << endl;}
}
// set容器排序
void test5() {// <> 里面,第二個參數可以指定排序規則,但是因為<>需要類型,所以這里需要用到仿函數set<int, MySort> s1;s1.insert(5);s1.insert(3);s1.insert(1);s1.insert(7);s1.insert(9);printSetIntSort(s1);
}//set 插入自定義數據類型
class Person {
public:Person(string name, int age) {this->m_name = name;this->m_age = age;}string m_name;int m_age;/*bool operator== (Person& p1) {if (this->m_age==p1.m_age && this->m_name == p1.m_name) {return true;}return false;}*/
};//自定義數據類型指定排序規則時所有成員都得是const成員
class compare01
{
public:bool operator()(const Person& p1, const Person& p2)const{return p1.m_age > p2.m_age;}
};void test6() {set<Person, compare01> s1; Person p1("大娃", 25);Person p2("二娃", 20);Person p3("五娃", 15);Person p4("三娃", 22);Person p5("爺爺", 55); // 這里如果爺爺的年齡和大娃一樣,就無法插入到s1,當中, 應該用的是年齡做判斷s1.insert(p1);s1.insert(p2);s1.insert(p3);s1.insert(p4);s1.insert(p5);for (set<Person>::iterator it1 = s1.begin(); it1 != s1.end(); ++it1) {cout << "name is " << (*it1).m_name << " age is" << (*it1).m_age << endl;}cout << "-----" << endl;//s1.erase(p5);for (set<Person>::iterator it1 = s1.begin(); it1 != s1.end(); ++it1) {cout << "name is " << (*it1).m_name << " age is" << (*it1).m_age << endl;}Person p6("三娃", 22);cout << "-----" << endl;s1.erase(p6); // Person類就算沒有重載 == 運算符,也能把三娃刪除掉// set容器刪除元素,判斷是否相等, 是通過compare01來判斷的for (set<Person>::iterator it1 = s1.begin(); it1 != s1.end(); ++it1) {cout << "name is " << (*it1).m_name << " age is" << (*it1).m_age << endl;}
}
int main()
{//test1();//test2();//test3();//test4();	//test5();test6();return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/382570.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/382570.shtml
英文地址,請注明出處:http://en.pswp.cn/news/382570.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++ STL容器之map 簡單使用

3.8.2.1 map構造函數 map<T1, T2> mapTT;//map默認構造函數: map(const map &mp);//拷貝構造函數3.8.2.2 map賦值操作 map& operator(const map &mp);//重載等號操作符 swap(mp);//交換兩個集合容器3.8.2.3 map大小操作 size();//返回容器中元素的數目 empty…

Manacher算法圖解

看了好久的Manacher算法&#xff0c;覺得還是要自己畫一遍&#xff0c;自己把代碼寫一遍才能理解 下面分享一下&#xff0c;如果有錯&#xff0c;希望指正 簡陋版本的&#xff0c;但是他基本只是做到了求取最長回文字符串&#xff0c;嚴格來說它并不是Manacher’s Algorithm-…

Flink 客戶端操作命令及可視化工具

Flink提供了豐富的客戶端操作來提交任務和與任務進行交互。下面主要從Flink命令行、Scala Shell、SQL Client、Restful API和 Web五個方面進行整理。 在Flink安裝目錄的bin目錄下可以看到flink&#xff0c;start-scala-shell.sh和sql-client.sh等文件&#xff0c;這些都是客戶…

ySQL挑戰搭建一個簡易的成績管理系統的數據庫

文章為自己搜索網上資源&#xff0c;再在這里進行整理&#xff0c;所以標注為轉載 [實驗步驟](https://www.shiyanlou.com/courses/reports/1347700) 總結做實驗注意事項&#xff1a; 1.添加主鍵 2.主鍵和外鍵的關系 3.注意自增的書寫添加 mysql 如何修改、添加、刪除表主鍵…

網絡之DNS協議圖解

DNS是計算機域名系統 (Domain Name System) 域名系統采用類似目錄樹的等級結構。 域名服務器是指保存有該網絡中所有主機的域名和對應IP地址&#xff0c;并具有將域名轉換為IP地址功能的服務器。 域名服務器為客戶機/服務器模式中的服務器方&#xff0c;它主要有兩種形式&am…

C++ 謂詞,

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <vector> #include <algorithm> using namespace std;class GreaterThen20 { public:bool operator()(int val){return val > 20;} };//一元謂詞 void test01() {vector<int>v;v.push…

網絡之ARP

地址解析協議&#xff0c;即ARP&#xff08;Address Resolution Protocol&#xff09;&#xff0c;是根據IP地址獲取物理地址的一個TCP/IP協議。 主機發送信息時將包含目標IP地址的ARP請求廣播到網絡上的所有主機&#xff0c;并接收返回消息&#xff0c;以此確定目標的物理地址…

C++ 內建函數對象

STL內建了一些函數對象。分為:算數類函數對象,關系運算類函數對象&#xff0c;邏輯運算類仿函數。這些仿函數所產生的對象&#xff0c;用法和一般函數完全相同&#xff0c;當然我們還可以產生無名的臨時對象來履行函數功能。使用內建函數對象&#xff0c;需要引入頭文件 functi…

網絡之ICMP協議

ICMP 主要功能&#xff1a; 確認IP包是否成功送達目標地址通知在發送過程當中IP包被廢棄的具體原因改善網絡設置等 在IP通信中如果某個IP包因為某種原因未到達目標地址&#xff0c;那么這個原因由ICMP通知。 過程&#xff08;圖解TCP/IP&#xff09; ICMP類型 常見的&am…

C++ 常用算法之遍歷

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <algorithm> #include <vector> #include <functional> using namespace std;/* 遍歷算法 遍歷容器元素 param beg 開始迭代器 param end 結束迭代器 param _callback 函數回調或者函數…

網絡之NAT協議

由來&#xff1a; 2011年2月3日中國農歷新年&#xff0c; IANA對外宣布&#xff1a;IPv4地址空間最后5個地址塊已經被分配給下屬的5個地區委員會。2011年4月15日&#xff0c;亞太區委員會APNIC對外宣布&#xff0c;除了個別保留地址外&#xff0c;本區域所有的IPv4地址基本耗盡…

C++ 常用查找算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <algorithm> using namespace std; #include <vector> #include <string> #include <functional> /* find算法 查找元素 param beg 容器開始迭代器 param end 容器結束迭代器 para…

CentOS7卸載并安裝mysql教程

MySQL安裝 先卸載其他 刪除Mysql yum remove mysql mysql-server mysql-libs mysql-server;find / -name mysql 將找到的相關東西delete掉(rm -rf /var/lib/mysql)&#xff1b;rpm -qa|grep mysql(查詢出來的東東yum remove掉) rm /etc/my.cnf查看是否還有mysql軟件&#x…

C++ 常用排序算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #include <algorithm> #include <vector> #include <functional> #include <ctime> /* merge算法 容器元素合并&#xff0c;并存儲到另一容器中 這兩個容器 必須也是…

排序穩定性的意義

首先&#xff0c;為什么會有排序算法穩定性的說法&#xff1f;只要能排好不就可以了嗎&#xff1f; 看例子 第1行是數字2 記作 1 2 第2行是數字4 記作 2 4 第3行是數字2 記作 3 2 排序后的結果&#xff08;如果看不懂命令的意思&#xff0c;參照這個博客&#xff09; 那么引入…

C++ 常用拷貝和替換算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std;/* copy算法 將容器內指定范圍的元素拷貝到另一容器中 param beg 容器開始迭代器 param end 容器結束迭代器 p…

防火墻的基礎知識入門

文章目錄防火墻基于實現方式&#xff0c;防火墻的發展分為四個階段:Linux 環境中主要的防火墻形式TCP wrappers~~詳解~~ 粗解Tcp wrappers的認識它的基本過程是這樣的&#xff1a;iptable攻擊和防御DDOS 攻擊常見的可能受到 DDOS 攻擊體現的癥狀有&#xff1a;而常見的 DDOS 攻…

C++ 常用算數生成算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <vector> using namespace std; #include <algorithm> //不好使 #include <numeric> //好使 #include <iterator> /* accumulate算法 計算容器元素累計總和 param beg 容器開始迭代…

fork()請問下面的程序一共輸出多少個“A”?多少個-?

題目&#xff1a;請問下面的程序一共輸出多少個“-”&#xff1f; #include #include #include int main(void) { int i; for(i0; i<2; i){ fork(); printf("-"); } return 0; } 解析&#xff1a;一共輸出8個。 首先程序一開始&am…

C++ 常用集合算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <algorithm> #include <vector> #include <iterator> using namespace std;/* set_intersection算法 求兩個set集合的交集 注意:兩個集合必須是有序序列 param beg1 容器1開始迭代器 par…