STL中set求交集、并集、差集的方法

并集(http://zh.cppreference.com/w/cpp/algorithm/set_union)

交集(http://zh.cppreference.com/w/cpp/algorithm/set_intersection)

差集(http://zh.cppreference.com/w/cpp/algorithm/set_difference)

inserter(http://zh.cppreference.com/w/cpp/iterator/inserter)

back_inserter(http://zh.cppreference.com/w/cpp/iterator/back_inserter)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 123;
int n;
int num[maxn];
int main(){
    set<int> a, b;
    vector<int> c;
    a =   {2,  4,   6};
    b = {1,2,3,4,5};
    //传入的a,b不一定是set, 但一定要有序
    set_union(a.begin(), a.end(), b.begin(), b.end(), back_inserter(c));//并集
    for(int n : c) cout << n << " "; puts("");
    c.clear();



    set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(c));//交集
    for(int n : c) cout << n << " "; puts("");
    c.clear();

    set_difference(a.begin(), a.end(), b.begin(), b.end(), back_inserter(c)); //差集(b中属于a的元素去掉)
    for(int n : c) cout << n << " "; puts("");
    c.clear();
}

STL中set求交集、并集、差集的方法

原文链接: https://www.cnblogs.com/Jadon97/p/8320926.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/268069

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 下午7:03
下一篇 2023年2月14日 下午7:05

相关推荐