首页 > 其他 > 详细

1002. Set

时间:2016-10-22 14:34:44      阅读:240      评论:0      收藏:0      [点我收藏+]

#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

原文:http://www.cnblogs.com/sysu-eeman-yang/p/5987440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!