STL中关于Set的部分功能实现

#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;
}

  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 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值