STL源碼剖析 Set相關算法 并集 set_union|交集 set_intersection|差集 set_difference |對稱差集 set_symmetric_difference

注意事項

  • 四種相關算法:并集、交集、差集、對稱差集
  • 本章的四個算法要求元素不可以重復并且經過了排序
  • 底層接受STL的set/multiset容器作為輸入空間
  • 不接受底層為hash_set和hash_multiset兩種容器

并集 set_union

  • s1 U s2
  • 考慮到s1 和 s2中每個元素都不唯一,如果單個元素在S1中出現了m次,在s2中出現了n次,那么該數值在并集區間內出現的次數是 max(n,m),假設m小于n。那么m個數來自s1,m-n個數來自s2
  • 穩定算法 相對次序不會改變
  • 版本一使用 operator <?
  • 版本二使用 comp 進行比較
  • 注意:set已經排好順序
template <class InputIterator1,class InputIterator2,class OutputIterator>
OutputIterator set_union(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result){//如果均沒有到達末尾//在兩個區間內分別移動迭代器,首先將元素較小的數值(假設為A區)記錄于目標區,移動A區迭代器使其前進,同一時間B區迭代器保持不變//如果兩個迭代器指向的元素的大小是一致的,均移動兩個迭代器while(true){//只要兩個區間中有一個到達末尾就需要結束循環//將尚未到達尾端的區間的所有剩余元素拷貝到目的端//此刻的[first1,last1)和[first2,last2)之中有一個是空白的區間if (first1==last1)return std::copy(first2,last2,result);if (first2==last2)return std::copy(first1,last1,result);if (*first1 < *first2){*result = *first1;++first1;} else if(*first1 > *first2){*result = *first2;++first2;} else{ //*first1 == *first2*result = *first1;++first1;++first2;}++result;}}
int main(){int first[] = {5,10,15,20,25};int second[] = {50,40,30,20,10};std::vector<int>v(10);std::vector<int>::iterator it;std::sort(first,first+5);      //5,10,15,20,25std::sort(second,second+5);  //10,20,30,40,50it = std::set_union(first,first+5,second,second+5,v.begin()); // 5 10 15 20 25 30 40 50  0  0v.resize(it-v.begin()); // 5 10 15 20 25 30 40 50std::cout << "The union has " << (v.size()) << " elements:\n";for (auto tmp : v) {std::cout << tmp << " ";}std::cout << std::endl;
}///Users/chy/Desktop/exceptional/cmake-build-debug/exceptional
//The union has 8 elements:
//5 10 15 20 25 30 40 50
//
//Process finished with exit code 0

交集 set_intersection

  • 構造元素的交集,即同時出現在兩個集合中的元素
  • 返回迭代器指向輸出區間的尾端?
  • 考慮到s1 和 s2中每個元素都不唯一,如果單個元素在S1中出現了m次,在s2中出現了n次,那么該數值在交集區間內出現的次數是 min(n,m)
  • 穩定算法 相對次序不會改變
  • 版本一使用 operator <?
  • 版本二使用 comp 進行比較
  • 注意:set已經排好順序
template <class InputIterator1,class InputIterator2,class OutputIterator>
OutputIterator set_intersection(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result){while(first1!=last1 && first2!=last2){if (*first1 < *first2)++first1;else if (*first1 > *first2){++first2;} else{*result = *first1;++first1;++first2;++result;}}return result;
}
int main(){int first[] = {5,10,15,20,25};int second[] = {50,40,30,20,10};std::vector<int>v(10);std::vector<int>::iterator it;std::sort(first,first+5);      //5,10,15,20,25std::sort(second,second+5);  //10,20,30,40,50it = std::set_intersection(first,first+5,second,second+5,v.begin()); // 10 20 0  0  0  0  0  0  0  0v.resize(it-v.begin()); // 10 20std::cout << "The intersection has " << (v.size()) << " elements:\n";for (auto tmp : v) {std::cout << tmp << " ";}std::cout << std::endl;
}///Users/chy/Desktop/exceptional/cmake-build-debug/exceptional
//The union has 2 elements:
//10 20
//
//Process finished with exit code 0

差集 set_difference

  • ?構造集合S1 - S2。此集合包含出現于S1但是不出現于S2的每一個元素
  • 返回迭代器指向輸出區間的尾端?
  • 考慮到s1 和 s2中每個元素都不唯一,如果單個元素在S1中出現了m次,在s2中出現了n次,那么該數值在交集區間內出現的次數是 max(m-n,0)
  • 穩定算法 相對次序不會改變
  • 版本一使用 operator <?
  • 版本二使用 comp 進行比較
  • 注意:set已經排好順序
template <class InputIterator1,class InputIterator2,class OutputIterator>
OutputIterator set_difference(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result){while(first1!=last1 && first2!=last2){if (*first1 < *first2){*result = *first1;++result;++first1;}else if (*first1 > *first2){++first2;} else{++first1;++first2;}}return std::copy(first1,last1,result);
}
int main(){int first[] = {5,10,15,20,25};int second[] = {50,40,30,20,10};std::vector<int>v(10);std::vector<int>::iterator it;std::sort(first,first+5);      //5,10,15,20,25std::sort(second,second+5);  //10,20,30,40,50it = std::set_difference(first,first+5,second,second+5,v.begin()); // 5 15 25  0  0  0  0  0  0  0v.resize(it-v.begin()); // 5 15 25std::cout << "The difference has " << (v.size()) << " elements:\n";for (auto tmp : v) {std::cout << tmp << " ";}std::cout << std::endl;
}///Users/chy/Desktop/exceptional/cmake-build-debug/exceptional
//The union has 2 elements:
//5 15 25
//
//Process finished with exit code 0

對稱差集 set_symmetric_difference

  • 構造出 (s1 - s2) U (s2 - s1)
  • 打印出現于s1但是不出現于s2 以及出現于s2但是不出現于s1的每一個元素
  • 考慮到s1 和 s2中每個元素都不唯一,如果單個元素在S1中出現了m次,在s2中出現了n次,那么該數值在交集區間內出現的次數是 | n - m|
  • 穩定算法 相對次序不會改變
  • 版本一使用 operator <?
  • 版本二使用 comp 進行比較
  • 注意:set已經排好順序
template <class InputIterator1,class InputIterator2,class OutputIterator>
OutputIterator set_symmetric_difference(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result){while (true){//以下兩個條件不會同時成立 即if (first1 == last1){return std::copy(first2,last2,result);}if (first2 == last2){return std::copy(first1,last1,result);}if (*first1 < *first2){*result = *first1;++result;++first1;}else if (*first1 > *first2){*result = *first2;++result;++first2;} else{++first1;++first2;}}
}
int main(){int first[] = {5,10,15,20,25};int second[] = {50,40,30,20,10};std::vector<int>v(10);std::vector<int>::iterator it;std::sort(first,first+5);      //5,10,15,20,25std::sort(second,second+5);  //10,20,30,40,50it = std::set_symmetric_difference(first,first+5,second,second+5,v.begin()); //5 15 25 30 40 50  0  0  0  0v.resize(it-v.begin()); // 5 15 25 30 40 50std::cout << "The set_symmetric_difference has " << (v.size()) << " elements:\n";for (auto tmp : v) {std::cout << tmp << " ";}std::cout << std::endl;
}///Users/chy/Desktop/exceptional/cmake-build-debug/exceptional
//The union has 2 elements:
//5 15 25 30 40 50 
//
//Process finished with exit code 0

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

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

相關文章

python sqlserver 數據操作_python對Excel數據進行讀寫操作

python對Excel數據進行讀寫操作將學習到的基礎操作記錄在這里&#xff0c;便與復習查看1.python讀取Excel工作簿、工作表import xlrd # 讀取工作簿 wbxlrd.open_workbook(招生表.xls) # 讀取工作簿下所有的工作表 wswb.sheets() # 讀取工作簿下所有工作表名稱 wsnamewb.sheet_n…

Arrays數組工具類

介紹 代碼 package lesson.l8_arrays;import java.util.Arrays;/*** Illustration** author DengQing* version 1.0* datetime 2022/6/23 16:53* function Arrays數組工具類*/ public class ArraysUtil {public static void main(String[] args) {int[] arr1 new int[]{1, 12…

通過解析URL實現通過Wifi的用戶查找

使用鏈接 遇見數據倉庫|遇見工具|IP地址精確查詢|WIFI精確查詢|在線語音識別|夢幻藏寶閣估價|福利資源|自定義導航-met.redhttps://sina.lt/ 操作步驟 打開第一個鏈接&#xff0c;點擊高精度IP定位&#xff0c;然后點擊右上角&#xff0c;創建一個Key&#xff0c;隨便輸入一…

anaconda中怎么sh_【好工具】 深度學習煉丹,你怎么能少了這款工具!JupyterLab 遠程訪問指南...

歡迎來到【好工具】專欄&#xff0c;本次我們給介紹一款可以進行遠程深度學習煉丹的工具 JupyterLab 及其配置流程&#xff0c;幫助讀者在本地進行調試&#xff0c;Max 開發效率。作者 & 編輯 | Leong導言不知道讀者們有沒有發現&#xff0c;如果你用 Anaconda 中的 Notebo…

java 類和對象 屬性和行為 成員變量和局部變量

概念 使用 案例 public class PersonText {public static void main(String[] args) {Person person new Person();person.name "dq";person.age 11;person.eat("番茄炒蛋");} }class Person {/*** 姓名*/String name;/*** 年齡*/Integer age;/*** 方…

STL源碼剖析 數值算法 heap算法

算法 adjacent_findcountcount_iffindfind_iffind_endfor_eachgenerategenerate_nincludesmax_elementmergemin_elementpartitionremoveremoveremove_copyremove_ifremove_copy_ifreplacereplace_copyreplace_ifreplace_copy_ifreversereverse_copyrotaterotate_copysearchsea…

java 學生對象數組

題目 代碼 package lesson.l10_oop;/*** Illustration** author DengQing* version 1.0* datetime 2022/7/1 9:57* function*/ public class Student {int number;int state;int score;public static final int NUM 20;public static void main(String[] args) { // 對…

STL源碼剖析 lower_bound | upper_bound | binary_search

lower_bound 二分查找的一種版本&#xff0c;試圖在已經排序的區間內查找元素value&#xff0c;如果區間內存在和value數值相等的元素&#xff0c;便返回一個迭代器&#xff0c;指向其中的第一個元素。如果沒有數值相等的元素&#xff0c;會返回假設這個元素存在的前提下應該出…

java能調用python嗎_如何使用運行時在Java中調用python程序 - java

我想用來自Java的參數調用python程序。但是我的輸出是空白。代碼在這里。 Python代碼在這里&#xff1a; import sys print(sys.argv[1]) Java代碼在這里&#xff1a; public class PrintNumber{ public static void main(String[] args){ Process proc; try { proc Runtime.g…

java 匿名對象

概念 代碼 package lesson.l10_oop;/*** Illustration** author DengQing* version 1.0* datetime 2022/7/1 13:39* function 匿名對象*/ public class Anonymous {public static void main(String[] args) { // 用法1new Teacher().say("dq");new Teacher()…

STL源碼剖析 第七章 仿函數(函數對象)

函數對象&#xff1a;具有函數性質的對象使得用戶像使用函數一樣使用它一般函數提供兩個版本&#xff0c;第一個版本使用operator < ;第二版本需要用戶 指定某種操作第二版本就是設計一個函數&#xff0c;將函數指針作為算法的一個參數&#xff1b;或者將函數操作設計成為一…

開源合同管理系統_「物聯網架構」最適合物聯網的開源數據庫

物聯網產生大量的數據&#xff0c;包括流數據、時間序列數據、RFID數據、傳感數據等。要有效地管理這些數據&#xff0c;就需要使用數據庫。物聯網數據的本質需要一種不同類型的數據庫。以下是一些數據庫&#xff0c;當與物聯網一起使用時&#xff0c;會給出非常好的結果。物聯…

java 方法重載

概念 代碼 package lesson.l10_oop;/*** Illustration** author DengQing* version 1.0* datetime 2022/7/1 14:31* function 方法重載*/ public class Load {public static void main(String[] args) {Load load new Load();load.mOL(4);load.mOL(4, 5);load.mOL("ff&qu…

STL源碼剖析 第八章 配接器

設計模式&#xff1a;將一個類的接口轉化為另外一個類的接口 配接器的概觀和分類 改變仿函數接口 函數配接器 &#xff1b;queue和stack 通過修飾deque函數接口來實現改變容器接口 容器配接器 &#xff1b; insert、reverse、iostream 等iterators他們的接口可以由ite…

python中random庫_python標準庫之random模塊

Python中的random模塊用于生成隨機數。 下面具體介紹random模塊的功能&#xff1a; 1.random.random() #用于生成一個0到1的 隨機浮點數&#xff1a;0< n < 1.0 1 import random 2 a random.random() 3 print (a)2.random.uniform(a,b) #用于生成一個指定范圍內的隨機符…

java 可變個數形參

概念 案例 package lesson.l10_oop;/*** Illustration** author DengQing* version 1.0* datetime 2022/7/1 14:53* function 可變個數形參*/ public class ChangeableFormalParameter {public static void main(String[] args) {ChangeableFormalParameter parameter new Ch…

C++標準庫 第七章 STL迭代器

迭代器 能力&#xff1a;行進和存取的能力Input迭代器 一次一個向前讀取元素&#xff0c;按此順序一個一個返回元素例子&#xff1a;從標準輸入裝置(鍵盤) 讀取數據&#xff0c;同一個數據不會被讀取兩次&#xff0c;流水一樣&#xff0c;指向的是邏輯位置使用前置式遞增運算…

nacos集群的ap cp切換_阿里Nacos-配置-多環境

多環境的配置隔離是配置中心最基礎的一個功能之一。不同的環境配置的值不一樣&#xff0c;比如數據庫的信息&#xff0c;業務的配置等。Spring Boot 多環境配置首先我們來回顧下在Spring Boot中用配置文件的方式怎么進行環境的隔離。默認我們都會創建一個application.propertie…

java 值傳遞機制

說明 案例1 案例2 案例3 案例4 案例5 案例6 package lesson.l11_oop2;/*** Illustration** author DengQing* version 1.0* datetime 2022/7/2 21:24* function 將對象作為參數傳遞給方法*/ public class Circle {double radius;public double findArea() {return Math.PI * Ma…

密碼學專題 非對稱加密算法指令概述 RSA

非對稱加密算法也稱為公開密鑰算法&#xff0c;其解決了對稱加密算法密鑰需要預分配的難題&#xff0c;使得現代密碼學的研究和應用取得了重大發展。非對稱加密算法的基本特點如下: 加密密鑰和解密密鑰不相同;密鑰對中的一個密鑰可以公開(稱為公開密鑰);根據公開密鑰很難推算出…