#include<iostream>
#include<algorithm>
#include<iterator>
#include<set>
#include<string>
using namespace std;
void main(){

    const int N=6;
    string s1[N]={"buffoon","thinkers","for","heavy","can","for"};
    string s2[N]={"metal","any","food","elegant","deliver","for"};

    set<string>A(s1,s1+N);
    set<string>B(s2,s2+N);
    ostream_iterator<string,char>out(cout," ");
    cout<<"set A:";
    copy(A.begin(),A.end(),out);
    cout<<endl<<"set B:";
    copy(B.begin(),B.end(),out);
    cout<<endl;

    cout<<"union of A and B:\n";
    set_union(A.begin(),A.end(),B.begin(),B.end(),out);
    cout<<endl;

    cout<<"intersection of A and B:\n";
    set_intersection(A.begin(),A.end(),B.begin(),B.end(),out);
    cout<<endl;

    cout<<"difference of A and B:\n";
    set_difference(A.begin(),A.end(),B.begin(),B.end(),out);
    cout<<endl;

    set<string>C;
    cout<<"set C:\n";
    set_union(A.begin(),A.end(),B.begin(),B.end(),insert_iterator<set<string> >(C,C.begin()));
    copy(C.begin(),C.end(),out);
    cout<<endl;

    string s3("grungy");
    C.insert(s3);
    cout<<"set C after insertion:\n";
    copy(C.begin(),C.end(),out);
    cout<<endl;

    cout<<"showing a range:\n";
    copy(C.lower_bound("ghost"),C.upper_bound("spook"),out);
    cout<<endl;



}

posted on 2013-02-23 15:08  叶城宇  阅读(153)  评论(0编辑  收藏  举报