1. 程式人生 > >STL集合的並集、交集、差集、對稱差集

STL集合的並集、交集、差集、對稱差集

STL一共提供了四種set相關的演算法,分別是並集(union),交集(intersection),差集(difference),和對稱差集(symmetric difference)。
STL的這四個演算法所接受的set必須是有序區間,元素可以重複出現。即他們只能接受set/multiset容器作為輸入區間。使用的條件是有序容器,所以 vector在被sort了之後是可以使用的,set也是可以使用的。

標頭檔案:algorithm

最後使用的時候注意要提前分配好最後的盛放容器,其大小最好是兩個操作容器的和,然後需要根據返回的迭代器resize一下,看下面的例子。

1.set_unoin(求並集)

演算法set_union可構造兩個S1,S2這兩個集合的並集,這個並集裡面包含了S1和S2這兩個集合裡面的所有元素。set_union的返回值是一個迭代器,指向輸出區間的尾端。由於multiset中S1和S2內的元素不需要唯一,所以如果某個值在S1出現n次,在S2出現m次,則這個值會在並集中出現max(m,n)次。

set_union(A.begin(),A.end(),B.begin(),B.end(),inserter( C1 , C1.begin() ) );前四個引數依次是第一的集合的頭尾,第二個集合的頭尾。第五個引數的意思是將集合A、B取合集後的結果存入集合C中。

#include <iostream>
#include<set>
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
    int num1[]={1,2,3,4,5,6,7,8,9};
    int num2[]={10,5,6,3,1,15,7,8};
    set<int>a(num1,num1+6);
    set<int>b(num2,num2+8);
    set<int>::iterator ita=a.begin();
    set<int>::iterator itb=b.begin();
    for(;ita!=a.end();ita++)
        cout<<*ita<<" ";
    cout<<endl;
    for(;itb!=b.end();itb++)
        cout<<*itb<<" ";
    cout<<endl;
    set<int>c;
    set_union(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
    for(set<int>::iterator itc=c.begin();itc!=c.end();itc++)
    {
        cout<<*itc<<" ";
    }
    cout<<endl;
    return 0;
}

輸出:   1 2 3 4 5 6

             1 3 5 6 7 8 10 15

            1 2 3 4 5 6 7 8 10 15

2.set_intersection(求交集)

演算法set_intersection是一種可以構造兩個集合S1,S2交集的演算法,這個交集裡面包含了S1和S2共有的元素。由於multiset中元素可以重複出現,因此如果某個值在S1出現n次,在S2出現m次,則在交集中出現min(n,m)次。

set_intersection(A.begin(),A.end(),B.begin(),B.end(),inserter( C1 , C1.begin() ) );前四個引數依次是第一的集合的頭尾,第二個集合的頭尾。第五個引數的意思是將集合A、B取合集後的結果存入集合C中。

程式碼:

#include <iostream>
#include<set>
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
    int num1[]={1,2,3,4,5,6,7,8,9};
    int num2[]={10,5,6,3,1,15,7,8};
    set<int>a(num1,num1+6);
    set<int>b(num2,num2+8);
    set<int>::iterator ita=a.begin();
    set<int>::iterator itb=b.begin();
    for(;ita!=a.end();ita++)
        cout<<*ita<<" ";
    cout<<endl;
    for(;itb!=b.end();itb++)
        cout<<*itb<<" ";
    cout<<endl;
    set<int>c;
    set_intersection(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
    for(set<int>::iterator itc=c.begin();itc!=c.end();itc++)
    {
        cout<<*itc<<" ";
    }
    cout<<endl;
    return 0;
}

輸出: 1 2 3 4 5 6

          1 3 5 6 7 8 10 15

          1 3 5 6

3.set_difference(求差集)

演算法set_difference可以構造S1和S2的差集,他能構造出S1-S2,表示出現S1但不出現於S2的每一個元素。由於multiset中元素的值可能重複,因此如果某個值在S1中出現n次,在S2中出現m次,那麼這個值在差集中會出現max(n-m,0)次,並且全部來自S1。

程式碼:

#include <iostream>
#include<set>
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
    int num1[]={1,2,3,4,5,6,7,8,9};
    int num2[]={10,5,6,3,1,15,7,8};
    set<int>a(num1,num1+6);
    set<int>b(num2,num2+8);
    set<int>::iterator ita=a.begin();
    set<int>::iterator itb=b.begin();
    for(;ita!=a.end();ita++)
        cout<<*ita<<" ";
    cout<<endl;
    for(;itb!=b.end();itb++)
        cout<<*itb<<" ";
    cout<<endl;
    set<int>c;
    set_difference(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
    for(set<int>::iterator itc=c.begin();itc!=c.end();itc++)
    {
        cout<<*itc<<" ";
    }
    cout<<endl;
    return 0;
}

輸出: 1 2 3 4 5 6

           1 3 5 6 7 8 10 15

           2 4

4.set_symmetric_difference(求對稱差集)

演算法set_symmetric_difference可構造S1和S2集合的對稱差集,所謂的對稱差集就是說(S1-S2)U(S2-S1),意思就是說在S1集合出現但不在S2集合出現,在S2集合出現但沒在S1集合出現的元素所組成的集合。由於multiset中元素可以重複出現,因此如果某個值在S1中出現n次,在S2中出現m次,那麼該值在對稱差集中出現|n-m|次。

程式碼:

#include <iostream>
#include<set>
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
    int num1[]={1,2,3,4,5,6,7,8,9};
    int num2[]={10,5,6,3,1,15,7,8};
    set<int>a(num1,num1+6);
    set<int>b(num2,num2+8);
    set<int>::iterator ita=a.begin();
    set<int>::iterator itb=b.begin();
    for(;ita!=a.end();ita++)
        cout<<*ita<<" ";
    cout<<endl;
    for(;itb!=b.end();itb++)
        cout<<*itb<<" ";
    cout<<endl;
    set<int>c;
    set_symmetric_difference(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
    for(set<int>::iterator itc=c.begin();itc!=c.end();itc++)
    {
        cout<<*itc<<" ";
    }
    cout<<endl;
    return 0;
}

輸出: 1 2 3 4 5 6

           1 3 5 6 7 8 10 15

           2 4 7 8 10 15

注:用完後清零集合!

————————————————————————————————————————————————————

例題:

C-Description

最近Jerry正在刻苦的學習STL中的set的功能函式,他發現set可以用現有的函式實現並、交、差、對稱差等功能,但是他沒有找到怎麼來比較兩個集合是否相等的功能函式,所以他想自己用其他的功能函式來實現能判斷兩個集合是否相等的功能函式。聰明的Jerry不一會就想到了解決辦法,現在他想拿這道題來考考你,看你有沒有他聰明。

Input

輸入有多組,每組資料有兩行,每一行都代表一個集合,每一行有若干個正整數(0<d<=2147483647),並且每行的最後一個數字都是0,代表該行資料的結束,且末尾的0不計入集合中。最後以EOF結束輸入。

Output

對於每組資料輸出都要輸入一個結果,如果兩個集合相等便輸出“YES”,否則輸出“NO”,每個結果佔一行

Sample Input

1 2 3 4 0 1 2 3 4 0 1 2 2 2 2 2 0 1 2 0 1 2 3 4 0 1 3 3 4 0

Sample Output

YES YES NO

思路:對稱差集為空

程式碼:

#include <iostream>
#include<set>
#include<iterator>
#include<algorithm>
using namespace std;
int main()
{
    int m;
    set<int> num1;
    set<int> num2;
    while(cin>>m)
    {
        if(m!=0)
        {
            num1.insert(m);
            while(cin>>m&&m)
                num1.insert(m);
        }
        int n;
        while(cin>>n&&n)
            num2.insert(n);
        set<int> num3;
        set_symmetric_difference(num1.begin(),num1.end(),num2.begin(),num2.end(),inserter(num3,num3.begin()));
        if(num3.size()==0)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
        num1.clear();
        num2.clear();
        num3.clear();

    }
    return 0;
}

相關推薦

STL集合交集對稱

STL一共提供了四種set相關的演算法,分別是並集(union),交集(intersection),差集(difference),和對稱差集(symmetric difference)。 STL的這四個演算法所接受的set必須是有序區間,元素可以重複出現。即他們只能接受se

bash技巧:求集合交集對稱

網上轉的,不錯,比使用awk容易點 給定兩個檔案 a.txt 和 b.txt ,每行是一個記錄(假設沒有重複),要求輸出兩集合的交集、並集、差集,輸出的結果只包括唯一項。交集定義為同時出現在兩個檔案中的記錄項,並集定義為出現在任何一個檔案中的記錄項,差集(A-B)定義為出

集合類(交集操作)

昨日,朋友拿來一份有些問題的原始碼,是一個集合類和幾個集合操作函式(並集、交集、差集),幫忙改了一下,剛轉C++不到半個月,因為只是為了演算法競賽而學C++,所以只好好學了C++偏向演算法層的知識,類只是過了一遍,沒有自己嘗試寫過,剛好拿來練練手,於是大刀闊斧的

獲取兩個DataTable之間的交集集合(ExceptIntersectUnion)

//獲取第一個資料來源DataTable         DataTable dt1 = DBHelper.GetDataTable("select top 10  ksdid,user_id,user_pwd from ksd_user_info");      

List的 交集操作

bject list imp st2 for add str class lis package com.zheting.collection.list; import java.util.ArrayList; import java.util.Arrays; impo

SQL 操作結果 -交集結果排序

nbsp 分享 字段名 運算 語法 http 測試數據 結構 put 操作結果集   為了配合測試,特地建了兩個表,並且添加了一些測試數據,其中重復記錄為東吳的人物。   表:Person_1魏國人物       表:Person_2蜀國人物      A、Union形成並

SQL查詢:交集

新建兩個表進行測試:  test_a ID name 1 曹操 2 郭嘉 3 孫權 4 周瑜

php中陣列的交集函式

計算陣列的並集 array_merge ( array array1[,arrayarray1[,array… ] )  將一個或多個數組的單元合併起來,一個數組中的值附加在前一個數組的後面。返回作為結果的陣列。 計算陣列的交集 array_intersect ( a

兩個集合的相等判斷交集

#include<iostream> using namespace std; const int Size=10; void Get(int a[]); void Judge(int a[],int b[]);//判斷兩函式是否相等 void Intersaction(int a[],

ES6陣列實現交集

let set1 = new Set(['a','b','c','d','e','f']); let set2 = new Set(['d','e','f','g','h','i']); //並集 let union = [...new Set([...set1,...set2])]; /

java求交集

package algorithm; import java.util.ArrayList; import java.util.List; public class SetOperation { public static void main(String[] args) {

兩個陣列的交集

const arr1 = [1,2,3];const arr2 = [3,4,5];// 並集const union = Array.from(new Set([...arr1,...arr2])); 

Python -- 兩個列表的交集

①差集 方法一: if __name__ == '__main__': a_list = [{'a' : 1}, {'b' : 2}, {'c' : 3}, {'d' : 4}, {'e' : 5

陣列取交集

package xuxin; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java

python求補集合交集

聯合( | ) 聯合(union)操作和集合的 OR(又稱可兼析取(inclusive disjunction))其實是等價的,兩個集合的聯合是一個新集合,該集合中的每個元素都至少是其中一個

List和set集合交集的區別retainAll,removeAlladdAll

set 、list集合的交集(retainAll)、差集(removeAll)是沒有區別的都是一樣的. set 、list集合的合集addAll是有區別的:set可以去重複;list不去重複 pub

MySQL 交集

建立兩個表 CREATE TABLE `object_a` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `oname` varchar(50) DE

STL 演算法vector/set集合-交集,,,對稱

針對這裡提及的四個集合運算必須特別注意: 1、第一個演算法需保證第一集合和第二集合有序,並從小到大排序,內部使用預設“<”操作符比較元素大小; 2、第二個演算法需保證第一集合和第二集合有序,排序方式參照Compare確定,內部使用Compare比較元素大小。 1 --

資料庫union交集intersectexcept

資料庫對兩個或多個結果集進行合併、取重、剔除操作時,可以通過UNION、INTERSECT、EXCEPT來實現。 所操作的結果集有如下限制條件: (1)所有查詢中的列數和列的順序必須相同。 (2)比較

集合運算(交集

問題描述   給出兩個整數集合A、B,求出他們的交集、並集以及B在A中的餘集。 輸入格式   第一行為一個整數n,表示集合A中的元素個數。   第二行有n個互不相同的用空格隔開的整數,表示集合A中的元素。   第三行為一個整數m,表示集合B中的元素個數。   第四行有m個