c++ STL中的set和multiset的使用

转载地址:http://blog.csdn.net/hnust_xiehonghao/article/details/7942541

C++ STL set和multiset的使用

std::set<int> s;那个s这个对象里面存贮的元素是从小到大排序的,(因为用std::less作为比较工具。)

1,set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就   像一个集合一样。所有的操作的都是严格在logn时间之内完成,效率非常高。

为什么是logn呢?

因为set和multiset都是基于红黑树实现的。

 set和multiset的区别是:set插入的元素不能相同,但是multiset可以相同。

   创建 multiset<ss> base;

   删除:如果删除元素a,那么在定义的比较关系下和a相等的所有元素都会被删除

   base.count( a ):set能返回0或者1,multiset是有多少个返回多少个.

   Set和multiset都是引用<set>头文件,复杂度都是logn

2,Set中的元素可以是任意类型的,但是由于需要排序,所以元素必须有一个序,即大小的比较关系,比如   整数可以用<比较.

3,自定义比较函数;

    include<set>

    typedef struct

    { 定义类型 }

    ss(类型名);

    struct cmp

    {

          bool operator()( const int &a, const int &b ) const

             { 定义比较关系<}

    };

    (运算符重载,重载<)

    set<ss> base; ( 创建一个元素类型是ss,名字是base的set )

    注:定义了<,==和>以及>=,<=就都确定了,STL的比较关系都是用<来确定的,所以必须通    过定义< --“严格弱小于”来确定比较关

4,set的基本操作:

begin()         返回指向第一个元素的迭代器

clear()         清除所有元素

count()         返回某个值元素的个数

empty()         如果集合为空,返回true

end()           返回指向最后一个元素的迭代器

equal_range()   返回集合中与给定值相等的上下限的两个迭代器

erase()         删除集合中的元素

find()          返回一个指向被查找到元素的迭代器

get_allocator() 返回集合的分配器

insert()        在集合中插入元素

lower_bound()   返回指向大于(或等于)某值的第一个元素的迭代器

key_comp()      返回一个用于元素间值比较的函数

max_size()      返回集合能容纳的元素的最大限值

rbegin()        返回指向集合中最后一个元素的反向迭代器

rend()          返回指向集合中第一个元素的反向迭代器

size()          集合中元素的数目

swap()          交换两个集合变量

upper_bound()   返回大于某个值元素的迭代器

value_comp()    返回一个用于比较元素间的值的函数

1:

set元素的插入:

[html]  view plain  copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <set>  
  4. using namespace std;  
  5. void printSet(set<int> s)  
  6. {  
  7.  set<int>::iterator i;  
  8.  for(i=s.begin();i!=s.end();i++)  
  9.         printf("%d ",*i);  
  10.  cout<<endl;  
  11. }  
  12. void main()  
  13. {  
  14.  //创建空的set对象,元素类型为int,  
  15.  set<int> s1;  
  16.  for (int i = 0; i <5 ; i++)  
  17.   s1.insert(i*10);  
  18.  printSet(s1);  
  19.  cout<<"s1.insert(20).second = "<<endl;;  
  20.  if (s1.insert(20).second)//再次插入20     
  21.   cout<<"Insert OK!"<<endl;  
  22.  else  
  23.   cout<<"Insert Failed!"<<endl;  
  24.  cout<<"s1.insert(50).second = "<<endl;  
  25.  if (s1.insert(50).second)  
  26.  {cout<<"Insert OK!"<<endl; printSet(s1);}  
  27.  else  
  28.   cout<<"Insert Failed!"<<endl;  
  29.  pair<set<int>::iterator, bool> p;  
  30.   p = s1.insert(60);  
  31.  if (p.second)  
  32.  {cout<<"Insert OK!"<<endl; printSet(s1);}  
  33.  else  
  34.   cout<<"Insert Failed!"<<endl;  
  35. }  
  36. 继续更新中  

 

2: set 的  empty  erase   删除特定元素

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <set>  
  3. using namespace std;  
  4. int main ()  
  5. {  
  6.   set<int> myset;  
  7.   myset.insert(20);  
  8.   myset.insert(30);  
  9.   myset.insert(10);  
  10.   while (!myset.empty())  
  11.   {  
  12.      cout <<" "<< *myset.begin();  
  13.      myset.erase(myset.begin());  
  14.   }  
  15.   cout << endl;  
  16.   return 0;  
  17. }  


 

[cpp]  view plain  copy
  1. //set::find    
  2. #include <iostream>  
  3. #include <set>  
  4. using namespace std;  
  5.   
  6. int main ()  
  7. {  
  8.   set<int> myset;  
  9.   set<int>::iterator it;  
  10.   for (int i=1; i<=5; i++) myset.insert(i*10);    // set: 10 20 30 40 50  
  11.   it=myset.find(20);  
  12.   myset.erase (it);  
  13.   myset.erase (myset.find(40));  
  14.   myset.erase (30);  
  15.   cout << "myset contains:";  
  16.   for (it=myset.begin(); it!=myset.end(); it++)  
  17.     cout << " " << *it;  
  18.   cout << endl;  
  19.   return 0;  
  20. }  

 lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个大于等于value 的值。  例如,有如下序列:  ia[]={12,15,17,19,20,22,23,26,29,35,40,51};  用值21调用lower_bound(),返回一个指向22的iterator。用值22调用lower_bound(),也返回一个指向22的iterator。

iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值> key的第一个元素。

[cpp]  view plain  copy
  1. // 6.set::lower_bound/upper_bound  
  2. #include <iostream>  
  3. #include <set>  
  4. using namespace std;  
  5.   
  6. int main ()  
  7. {  
  8.   set<int> myset;  
  9.   set<int>::iterator it,itlow,itup;  
  10.   
  11.   for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90  
  12.   
  13.   itlow=myset.lower_bound (30);                //    >=   
  14.   itup=myset.upper_bound (60);                 //     >               
  15.   printf("%d %d",*itlow,*itup);  //  30 70  
  16.   return 0;  
  17. }  


 

[cpp]  view plain  copy
  1. // 7.set::equal_elements  
  2. #include <iostream>  
  3. #include <set>  
  4. using namespace std;  
  5.   
  6. int main ()  
  7. {  
  8.   set<int> myset;  
  9.   pair<set<int>::iterator,set<int>::iterator> ret;  
  10.   
  11.   for (int i=1; i<=5; i++) myset.insert(i*10);   // set: 10 20 30 40 50  
  12.   
  13.   ret = myset.equal_range(30);  
  14.   
  15.   cout << "lower bound points to: " << *ret.first << endl;  
  16.   cout << "upper bound points to: " << *ret.second << endl;  
  17.   
  18.   return 0;  
  19. }   
  20.   
  21.   
  22. //lower bound points to: 30  
  23. //upper bound points to: 40  


 set结构体的应用

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<set>  
  3. using namespace std;  
  4. struct haha  
  5. {     
  6.     int a,b;      
  7.     char s;   
  8.     friend bool operator<(struct haha a,struct haha b)     
  9.     {     
  10.         return a.s<b.s;        
  11.     }     
  12. };  
  13. set<struct haha>element;  
  14. int main()  
  15. {     
  16.     struct haha a,b,c,d,t;    
  17.     a.a=1; a.s='b';   
  18.     b.a=2; b.s='c';   
  19.     c.a=4; c.s='d';   
  20.     d.a=3; d.s='a';  
  21.     element.insert(d);    
  22.     element.insert(b);    
  23.     element.insert(c);    
  24.     element.insert(a);    
  25.     set<struct haha>::iterator it;      
  26.     for(it=element.begin(); it!=element.end();it++)       
  27.         cout<<(*it).a<<" ";   
  28.     cout<<endl;     
  29.     for(it=element.begin(); it!=element.end();it++)       
  30.         cout<<(*it).s<<" ";   
  31. }  


集合的并集 交集  差集 等等 

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<string>  
  3. #include<set>  
  4. #include<iostream>  
  5. #include <algorithm>//包含  
  6. using namespace std;  
  7.   
  8. struct compare//自定义排序方式  
  9. {  
  10.     bool operator ()(string s1,string s2)  
  11.     {  
  12.         return s1>s2;  
  13.     }///自定义一个仿函数  
  14. };  
  15. int main()  
  16. {  
  17.     typedef  set<string,compare>  SET;  
  18.     SET s;//建立第一个集合  
  19.     s.insert(string("sfdsfd"));  
  20.     s.insert(string("apple"));  
  21.     s.insert(string("english"));  
  22.     s.insert(string("dstd"));  
  23.     cout<<"第一个集合s1为:"<<endl;  
  24.      set<string,compare>::iterator it = s.begin();  
  25.     while(it!=s.end())  
  26.         cout<<*it++<<"   ";  
  27.   
  28.     SET s2;//建立第二个集合  
  29.     s2.insert(string("abc"));  
  30.     s2.insert(string("apple"));  
  31.     s2.insert(string("english"));  
  32.     cout<<endl<<"第一个集合s2为:"<<endl;  
  33.     it = s2.begin();  
  34.     while(it!=s2.end())  
  35.         cout<<*it++<<"   ";  
  36.     cout<<endl<<endl;  
  37.       
  38.     string str[10];  
  39.     string *end =set_intersection(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//求交集,返回值指向str最后一个元素的尾端  
  40.     /*set_intersection包含于#include <algorithm>   头文件中  其中上面的不一定非要为set容器 也可以使数组 但是前提是要把2个数组都排好序才可以 
  41.     返回值是一个指向交集序列末尾的迭代器 至于是什么迭代器与第5个参数有关 如果是数组 返回为int的迭代器 */  
  42.     cout<<"s1,s2的交集为:"<<endl;  
  43.     string *first = str;  
  44.     while(first<end)  
  45.         cout <<*first++<<" ";  
  46.   
  47.   
  48.     cout<<endl<<endl<<"s1,s2的并集为:"<<endl;  
  49.     end =set_union(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//并集  
  50.     first = str;  
  51.     while(first<end)  
  52.         cout <<*first++<<" ";  
  53.   
  54.   
  55.     cout<<endl<<endl<<"s2相对于s1的差集:"<<endl;   
  56.     first = str;  
  57.     end = std::set_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//s2相对于s1的差集  
  58.     while(first<end)  
  59.         cout <<*first++<<" ";  
  60.   
  61.   
  62.     cout<<endl<<endl<<"s1相对于s2的差集:"<<endl;   
  63.     first = str;  
  64.     end = std::set_difference(s2.begin(),s2.end(),s.begin(),s.end(),str,compare());//s1相对于s2的差集  
  65.       
  66.     while(first<end)  
  67.         cout <<*first++<<" ";  
  68.     cout<<endl<<endl;  
  69.     first = str;  
  70.     end = std::set_symmetric_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//上面两个差集的并集  
  71.     while(first<end)  
  72.         cout <<*first++<<" ";  
  73.     cout<<endl;   
  74.   
  75. /* 
  76. set<int>   s3   ;    
  77. set<int>::iterator   iter   =   s3.begin()   ;    
  78. set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s3,iter));    
  79. copy(s3.begin(),s3.end(),   ostream_iterator<int>(cout,"   ")); 
  80. */  
  81. }  

另外一个实例

[cpp]  view plain  copy
  1. /*set_intersection()算法计算两个集合[start1, end1)和[start2, end2)的交集, 交集被储存在result中. 
  2.  
  3. 两个集合以序列的形式给出, 并且必须先按升序排好位置. 
  4.  
  5. set_intersection()的返回值是一个指向交集序列末尾的迭代器. 
  6.  
  7. set_intersection()以线性时间(linear time)运行. 
  8.  
  9. 如果严格弱排序函数对象cmp未指定, set_intersection()将使用<操作符比较元素. 
  10.  
  11. 范例 
  12. */  
  13.   
  14. // set_intersection example  
  15. #include <iostream>  
  16. #include <algorithm>  
  17. #include <vector>  
  18. using namespace std;  
  19.    
  20. int main () {  
  21.   int first[] = {5,10,15,20,25};  
  22.   int second[] = {50,40,30,20,10};  
  23.   vector<int> v(10);                           // 0  0  0  0  0  0  0  0  0  0  
  24.   vector<int>::iterator it;  
  25.    
  26.   sort (first,first+5);     //  5 10 15 20 25  
  27.   sort (second,second+5);   // 10 20 30 40 50  
  28.    
  29.   it=set_intersection (first, first+5, second, second+5, v.begin());  
  30.                                                // 10 20 0  0  0  0  0  0  0  0  
  31.    
  32.   cout << "intersection has " << int(it - v.begin()) << " elements.\n";  
  33.    
  34.   return 0;  
  35. }  
  36. /*输出: intersection has 2 elements*/  






 multiset的删除  重要

a.erase(x);//删除集合中所有的x
multiset<int>::iterrator it = a.find(x);
if (it != a.end())
{
    a.erase(it);
}
//这里是删除其中的一个x;  删除的是一个位置  而erase是删除所有位置


                
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值