#include<iostream>
#include<iterator>
#include<set>
using namespace std;
int main()
{
typedef set<int> S;
S my_set;
/*----insert value in set----*/
my_set.insert(1);
my_set.insert(7);
my_set.insert(5);
my_set.insert(4);
my_set.insert(1);//because 1 already exists in the set.so don't insert again
S::iterator It=my_set.begin();
cout<<"the set value is : ";
//the set default asc order
while(It!=my_set.end())
{
cout<<(*It)<<" ";
It++;
}
cout<<endl;
//return element count
cout<<"the set size is: "<<my_set.size()<<endl;
//return count of one element
cout<<"the set 5 count is: "<<my_set.count(5)<<endl;
cout<<"the set 6 count is: "<<my_set.count(6)<<endl;
/*----UNION AND Intersection----*/
S set1,set2;
int i=0;
while(i<6)
{
set1.insert(i);
i++;
}
//Union
set2.clear(); //the set must clear
set_union(my_set.begin(),my_set.end(),set1.begin(),set1.end(),insert_iterator<set<int> >(set2,set2.begin()));
It=set2.begin();
cout<<"the union is :";
while(It!=set2.end())
{
cout<<(*It)<<" ";
It++;
}
cout<<endl;
//Intersection
set2.clear(); //here must clear
set_intersection(my_set.begin(),my_set.end(),set1.begin(),set1.end(),insert_iterator<set<int> >(set2,set2.begin()));
It=set2.begin();
cout<<"the interserction is :";
while(It!=set2.end())
{
cout<<(*It)<<" ";
It++;
}
cout<<endl;
return 0;
}
#include<iterator>
#include<set>
using namespace std;
int main()
{
typedef set<int> S;
S my_set;
/*----insert value in set----*/
my_set.insert(1);
my_set.insert(7);
my_set.insert(5);
my_set.insert(4);
my_set.insert(1);//because 1 already exists in the set.so don't insert again
S::iterator It=my_set.begin();
cout<<"the set value is : ";
//the set default asc order
while(It!=my_set.end())
{
cout<<(*It)<<" ";
It++;
}
cout<<endl;
//return element count
cout<<"the set size is: "<<my_set.size()<<endl;
//return count of one element
cout<<"the set 5 count is: "<<my_set.count(5)<<endl;
cout<<"the set 6 count is: "<<my_set.count(6)<<endl;
/*----UNION AND Intersection----*/
S set1,set2;
int i=0;
while(i<6)
{
set1.insert(i);
i++;
}
//Union
set2.clear(); //the set must clear
set_union(my_set.begin(),my_set.end(),set1.begin(),set1.end(),insert_iterator<set<int> >(set2,set2.begin()));
It=set2.begin();
cout<<"the union is :";
while(It!=set2.end())
{
cout<<(*It)<<" ";
It++;
}
cout<<endl;
//Intersection
set2.clear(); //here must clear
set_intersection(my_set.begin(),my_set.end(),set1.begin(),set1.end(),insert_iterator<set<int> >(set2,set2.begin()));
It=set2.begin();
cout<<"the interserction is :";
while(It!=set2.end())
{
cout<<(*It)<<" ";
It++;
}
cout<<endl;
return 0;
}
result:
the set value is : 1 4 5 7
the set size is: 4
the set 5 count is: 1
the set 6 count is: 0
the union is :0 1 2 3 4 5 7
the interserction is :1 4 5