首页 > 代码库 > 1002. Set

1002. Set

#include<iostream>
#include<iomanip>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
class SetOperation

{

public:

// Add any codes you want here
SetOperation(int a[], int a_size, int b[], int b_size) {
    for(int it = 0; it < a_size; it++) {
        setA.insert(a[it]);
    }
    for(int it = 0; it < b_size; it++) {
        setB.insert(b[it]);
    }
}
set<int> Union() {
    set<int> un;
    for(set<int>::iterator it = setA.begin(); it != setA.end(); it++) {
        un.insert(*it); 
    }
    for(set<int>::iterator it = setB.begin(); it != setB.end(); it++) {
        for(set<int>::iterator j = setA.begin(); j != setA.end(); j++) {
            if(*j != *it) {
                un.insert(*it);
                break;
            }
        }
    }
    return un;
};

set<int> Intersetion() {
    set<int> inte;
    for(set<int>::iterator it = setB.begin(); it != setB.end(); it++) {
        for(set<int>::iterator j = setA.begin(); j != setA.end(); j++) {
            if(*j == *it) {
                inte.insert(*it);
                break;
            }
        }
    }
    return inte;
};

private:

set<int> setA;

set<int> setB;

};

 


void printSet(const set<int> &s) {

         bool first = true;

         for (set<int>::iterator it = s.begin(); it != s.end(); it++) {

                   if (!first) cout << " ";

                            else first = false;

                   cout << *it;

         }

         cout << endl;

}

 

int main(int argc, char *argv[]) {

int a[7] = {8, 7, 5, 3, 1, 4, 6};

int b[4] = {2, 3, 5, 4};

SetOperation s1(a, 7, b, 4);

printSet(s1.Union());

printSet(s1.Intersetion());

 

return 0;

}

1002. Set