日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL 之includes,set_intersection,set_union,set_difference,set_symmetric_difference

發布時間:2024/4/11 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL 之includes,set_intersection,set_union,set_difference,set_symmetric_difference 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

includes,set_intersection,set_union,set_difference,set_symmetric_difference都是集合論中的操作。這些算法都假定指定區間類的元素已經有序。

includes:A∈B

set_intersection:A∩B

set_union:A∪B

set_difference:A-B

set_symmetric_difference:(A-B)∪(B-A)


1,includes

聲明:

  • #include?<algorithm>??
  • template?<class?inputItr1,?class?inputItr2>??
  • bool?includes(inputItr1?first1,?inputItr1?last1,?inputItr2?first2,?inputItr2?last2);??
  • template?<class?inputItr1,?class?inputItr2,?class?binaryPredicate>??
  • bool?includes(inputItr1?first1,?inputItr1?last1,?inputItr2,binaryPredicate?op);??

  • 示例代碼:

  • #include?<iostream>??
  • #include?<list>??
  • ??
  • #include?<string>??
  • #include?<numeric>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • ??
  • #include?<algorithm>??
  • ??
  • using?namespace?std;??
  • ??
  • int?main()?{??
  • ????char?setA[5]?=?{'A','B','C','D','E'};??
  • ????char?setB[10]?=?{'A','B','C','D','E','F','I','J','K','L'};??
  • ????char?setC[5]?=?{'A','E','I','O','U'};??
  • ??
  • ????ostream_iterator<char>?screen(cout,?"?");??
  • ????cout?<<?"setA:"?<<?endl;??
  • ????copy(setA,setA+5,screen);??
  • ????cout?<<?endl;??
  • ????cout?<<?"setB:"?<<?endl;??
  • ????copy(setB,setB+10,screen);??
  • ????cout?<<?endl;??
  • ????cout?<<?"setC:"?<<?endl;??
  • ????copy(setC,setC+5,screen);??
  • ????cout?<<?endl;??
  • ??
  • ????if?(includes(setB,setB+10,setA,setA+5))??
  • ????{??
  • ????????cout?<<?"setA?is?a?subset?of?setB"?<<?endl;??
  • ????}?else?{??
  • ????????cout?<<?"setA?is?not?a?subset?of?setB"?<<?endl;??
  • ????}??
  • ??
  • ????if?(includes(setB,setB+10,setC,setC+5))??
  • ????{??
  • ????????cout?<<?"setC?is?a?subset?of?setB"?<<?endl;??
  • ????}?else?{??
  • ????????cout?<<?"setC?is?not?a?subset?of?setB"?<<?endl;??
  • ????}??
  • ??
  • ????return?0;??
  • }??

  • 運行結果:

    charList:
    a b c d e
    charList:
    A B C D E
    list
    2 8 5 1 7 11 3
    4 16 10 2 14 22 6
    list
    4 16 10 2 14 22 6


    2,set_intersection,set_union

    聲明:

  • #include?<algorithm>??
  • template?<class?inputItr1,?class?inputItr2,class?outputItr>??
  • outputItr?set_intersection(inputItr1?first1,?inputItr1?last1,?inputItr2?first2,inputItr2?last2,outputItr?destFirst);??
  • template?<class?inputItr1,?class?inputItr2,class?outputItr,class?binaryPredicate>??
  • outputItr?set_intersection(inputItr1?first1,?inputItr1?last1,?inputItr2?first2,inputItr2?last2,outputItr?destFirst,binaryPredicate?op);??
  • ??
  • template?<class?inputItr1,?class?inputItr2,class?outputItr>??
  • outputItr?set_union(inputItr1?first1,?inputItr1?last1,?inputItr2?first2,inputItr2?last2,outputItr?destFirst);??
  • template?<class?inputItr1,?class?inputItr2,class?outputItr,class?binaryPredicate>??
  • outputItr?set_union(inputItr1?first1,?inputItr1?last1,?inputItr2?first2,inputItr2?last2,outputItr?destFirst,binaryPredicate?op);??

  • 示例代碼:

  • #include?<iostream>??
  • #include?<list>??
  • ??
  • #include?<string>??
  • #include?<numeric>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • ??
  • #include?<algorithm>??
  • ??
  • using?namespace?std;??
  • ??
  • int?main()?{??
  • ????int?setA[5]?=?{2,4,5,7,8};??
  • ????int?setB[7]?=?{1,2,3,4,5,6,7};??
  • ????int?setC[5]?=?{2,5,8,8,15};??
  • ????int?setD[6]?=?{1,4,4,6,7,12};??
  • ????int?AunionB[10];??
  • ????int?AunionC[10];??
  • ????int?BunionD[15];??
  • ????int?AintersectB[10];??
  • ????int?AintersectC[10];??
  • ??
  • ????int?*?lastElement;??
  • ????ostream_iterator<int>?screen(cout,"?");??
  • ????cout?<<?"setA:"?<<?endl;??
  • ????copy(setA,setA+5,screen);??
  • ????cout?<<?endl;??
  • ????cout?<<?"setB:"?<<?endl;??
  • ????copy(setB,setB+7,screen);??
  • ????cout?<<?endl;??
  • ????cout?<<?"setC:"?<<?endl;??
  • ????copy(setC,setC+5,screen);??
  • ????cout?<<?endl;??
  • ????cout?<<?"setD:"?<<?endl;??
  • ????copy(setD,setD+6,screen);??
  • ????cout?<<?endl;??
  • ??
  • ????//?set_union??
  • ????lastElement?=?set_union(setA,setA+5,setB,setB+7,AunionB);??
  • ????cout?<<?"AunionB:"?<<?endl;??
  • ????copy(AunionB,lastElement,screen);??
  • ????cout?<<?endl;??
  • ??
  • ????lastElement?=?set_union(setA,setA+5,setC,setC+5,AunionC);??
  • ????cout?<<?"AunionC:"?<<?endl;??
  • ????copy(AunionC,lastElement,screen);??
  • ????cout?<<?endl;??
  • ??
  • ????lastElement?=?set_union(setB,setB+7,setD,setD+6,BunionD);??
  • ????cout?<<?"BunionD:"?<<?endl;??
  • ????copy(BunionD,lastElement,screen);??
  • ????cout?<<?endl;??
  • ??
  • ????//?set_intersection??
  • ????lastElement?=?set_intersection(setA,?setA+5,setB,setB+7,AintersectB);??
  • ????cout?<<?"AintersectB:"?<<?endl;??
  • ????copy(AintersectB,lastElement,screen);??
  • ????cout?<<?endl;??
  • ??
  • ????lastElement?=?set_intersection(setA,?setA+5,setC,setC+5,AintersectC);??
  • ????cout?<<?"AintersectC:"?<<?endl;??
  • ????copy(AintersectC,lastElement,screen);??
  • ????cout?<<?endl;??
  • ????return?0;??
  • }??
  • 運行結果:

    charList:
    a b c d e
    charList:
    A B C D E
    list
    2 8 5 1 7 11 3
    4 16 10 2 14 22 6
    list
    4 16 10 2 14 22 6


    3?set_difference,set_symmetric_difference

    聲明:
  • #include?<algorithm>??
  • template?<class?inputItr1,?class?inputItr2,class?outputItr>??
  • outputItr?set_difference(inputItr1?first1,?inputItr1?last1,?inputItr2?first2,inputItr2?last2,outputItr?destFirst);??
  • template?<class?inputItr1,?class?inputItr2,class?outputItr,class?binaryPredicate>??
  • outputItr?set_difference(inputItr1?first1,?inputItr1?last1,?inputItr2?first2,inputItr2?last2,outputItr?destFirst,binaryPredicate?op);??
  • ??
  • template?<class?inputItr1,?class?inputItr2,class?outputItr>??
  • outputItr?set_symmetric_difference(inputItr1?first1,?inputItr1?last1,?inputItr2?first2,inputItr2?last2,outputItr?destFirst);??
  • template?<class?inputItr1,?class?inputItr2,class?outputItr,class?binaryPredicate>??
  • outputItr?set_symmetric_difference(inputItr1?first1,?inputItr1?last1,?inputItr2?first2,inputItr2?last2,outputItr?destFirst,binaryPredicate?op);??

  • 示例代碼:
  • #include?<iostream>??
  • #include?<list>??
  • ??
  • #include?<string>??
  • #include?<numeric>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • ??
  • #include?<algorithm>??
  • ??
  • using?namespace?std;??
  • ??
  • int?main()?{??
  • ????int?setA[5]?=?{2,4,5,7,8};??
  • ????int?setB[7]?=?{3,4,5,6,7,8,10};??
  • ????int?setC[5]?=?{1,5,6,8,15};??
  • ??
  • ????int?AdifferenceC[5];??
  • ????int?BsymDiffC[10];??
  • ????int?*?lastElement;??
  • ????ostream_iterator<int>?screen(cout,"?");??
  • ????cout?<<?"setA:"?<<?endl;??
  • ????copy(setA,setA+5,screen);??
  • ????cout?<<?endl;??
  • ????cout?<<?"setB:"?<<?endl;??
  • ????copy(setB,setB+7,screen);??
  • ????cout?<<?endl;??
  • ????cout?<<?"setC:"?<<?endl;??
  • ????copy(setC,setC+5,screen);??
  • ????cout?<<?endl;??
  • ??
  • ????//?set_difference??
  • ????lastElement?=?set_difference(setA,setA+5,setC,setC+5,AdifferenceC);??
  • ????cout?<<?"AdifferenceC:"?<<?endl;??
  • ????copy(AdifferenceC,lastElement,screen);??
  • ????cout?<<?endl;??
  • ??
  • ????//?set_symmetric_differenc?對稱差??
  • ????lastElement?=?set_symmetric_difference(setB,setB+7,setC,setC+5,BsymDiffC);??
  • ????cout?<<?"BsymDiffC:"?<<?endl;??
  • ????copy(BsymDiffC,lastElement,screen);??
  • ????cout?<<?endl;??
  • ??????
  • ????return?0;??
  • }??
  • 運行結果:
    setA:
    2 4 5 7 8
    setB:
    3 4 5 6 7 8 10
    setC:
    1 5 6 8 15
    AdifferenceC:
    2 4 7
    BsymDiffC:
    1 3 4 7 10 15

    總結

    以上是生活随笔為你收集整理的STL 之includes,set_intersection,set_union,set_difference,set_symmetric_difference的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。