码迷,mamicode.com
首页 > 其他好文 > 详细

1002. Set

时间:2016-10-22 14:37:08      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:ons   include   space   print   add   public   ant   else   bool   

#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

标签:ons   include   space   print   add   public   ant   else   bool   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!