set集合容器

set集合容器

采用了红黑树(Red-Black Tree)的平衡二叉检索树的数据结构。
确保每个子树根节点的键值大于左子树所有节点的键值,而小于右子树所有节点的键值;另外,还得确保根节点左子树的高度与右子树的高度相等,这样,二叉树的高度最小,从而检索速度最快。
不会重复插入相同键值的元素,而采取忽略处理。

构造set集合的主要目的就是为了快速检索。

  • 平衡二叉检索树的检索使用中序遍历算法,检索效率高于vector、deque和list等容器。
  • 采用中序遍历算法可将键值由小到大遍历出来。
  • 对于set容器中的键值,不可直接去修改。因为如果把容器中的一个键值修改了,set容器会根据新的键值旋转子树,以保持新的平衡,这样,修改的键值很可能就不在原先那个位置上了。

multiset(多重集合容器)、map(映照容器)和multimap(多重映照容器)的内部结构也是平衡二叉检索树。

声明“#include ”; 包含了set和multiset两种容器的定义。

使用

创建set集合对象

//定义元素类型为int的集合对象s,当前没有任何元素 
//元素的排列采用默认的比较规则(由小到大),当然,可以自定义比较规则函数
 set<int> s;

元素的插入与中序遍历

采用insert()方法把元素插入集合中去,插入的具体规则在默认的比较规则(按元素值由小到大)下插入

如果自己指定了比较规则函数,则按自定义比较规则函数插入。

如果使用前向迭代器对集合中序遍历,其结果正好是元素排序的结果。

//插入了5个元素,但由于8有重复,第二次插入的8并没有执行
//第一次插入8,可以插入
     s.insert(8); 
     s.insert(1);
     s.insert(12);
     s.insert(6);
     s.insert(8); 
//第二次插入8,重复元素,不会插入
//中序遍历集合中的元素
     set<int>::iterator it; //定义前向迭代器
      //中序遍历集合中的所有元素
     for(it=s.begin(); it! =s.end(); it++)
     {
       cout<<*it<<" ";
     }
#include<iostream>
#include<set>
using namespace std;

int main(){
  	set<int> s;
  	s.insert(8);
  	s.insert(2);
  	s.insert(1);
  	s.insert(12);
  	set<int>::iterator it;
  	for(it=s.begin();it!=s.end();it++){
  		cout<<*it<<"\t";
	}
  	cout<<endl;
}

元素的反向遍历

使用反向迭代器reverse_iterator可以反向遍历集合,输出的结果正好是集合元素的反向排序结果。

需要用到rbegin()和rend()两个方法,它们分别给出了反向遍历的开始位置和结束位置。

//反向遍历集合中的元素
set<int>::reverse_iterator rit; //定义反向迭代器
for(rit=s.rbegin(); rit! =s.rend(); rit++)
{
    cout<<*rit<<" ";
}
#include<iostream>
#include<set>
using namespace std;

int main(){ 
  	set<int> s;
  	s.insert(8);
  	s.insert(2);
  	s.insert(1);
  	s.insert(12);
  	set<int>::reverse_iterator rit;
    //反向遍历
  	for(rit=s.rbegin();rit!=s.rend();rit++){
  		cout<<*rit<<"\t";
	}
  	cout<<endl;
}

元素的删除

集合具有高效的删除处理功能,并自动重新调整内部的红黑树的平衡。

删除的对象可以是某个迭代器位置上的元素、等于某键值的元素、一个区间上的元素和清空集合。

//删除键值为6的那个元素
s.erase(6);
//清空集合
s.clear();
//输出集合的大小,为0
cout<<s.size()<<endl;

元素的检索

使用find()方法对集合进行搜索,如果找到查找的键值,则返回该键值的迭代器位置,否则,返回集合最后一个元素后面的一个位置,即end()。

//定义前向迭代器
set<int>::iterator it; 
//查找键值为6的元素
it=s.find(6);
if(it! =s.end())//找到
 	cout<<*it<<endl;
else//没找到
    cout<<"not find it"<<endl;
#include<iostream>
#include<set>
using namespace std;

int main(){ 
  	set<int> s;
  	s.insert(8);
  	s.insert(2);
  	s.insert(1);
  	s.insert(12);
  	s.erase(12);
  	set<int>::iterator it;
	it=s.find(12);
	if(it!=s.end()){
		cout<<*it<<endl;
	}else{
		cout<<"can find it"<<endl;
	}
}

自定义比较函数

在定义集合的时候,如果没有指定比较函数,那么采用默认的比较函数,即按键值由小到大的顺序插入元素。

编写比较函数有两种方法。

  • 如果元素不是结构体,那么,可以编写比较函数。

    // 比较规则: 按键值由大到小的顺序
    //自定义比较函数myComp,重载“()”操作符
        struct myComp
        {
           bool operator()(const int &a, const int &b)
           {
    			return a>b;
          }
       };
    
    //采用的比较函数是myComp,定义set
    set<int, myComp> s;
    
    #include<iostream>
    #include<set>
    using namespace std;
    
    
    class myComp{
    	public:
    		bool operator ()(const int& a,const int&b){
    			return a>b;
    	}
    };
    int main(){ 
      	set<int,myComp> s;
      	s.insert(8);
      	s.insert(2);
      	s.insert(1);
      	s.insert(12);
    
      	set<int>::iterator it;
    	for(it=s.begin();it!=s.end();it++){
    		cout<<*it<<"\t";
    	}
    }
    
  • 如果元素是结构体,那么,可以直接把比较函数写在结构体内。

    struct Info
        {
           string name;
           float score;
        
         //重载“<”操作符,自定义排序规则
         bool operator < (const Info &a) const
         {
           //按score由大到小排列。
           //如果要由小到大排列,使用“<”号即可。
           return score>a.score;
         }
    };
    //采用自定义规则,定义元素类型为Info结构体的集合对象s
         set<Info> s;
    

    只能重载< 操作符

    #include<iostream>
    #include<set>
    using namespace std;
    
    
    struct myComp{		
    		int cost;
    
    		bool operator < (const myComp& b) const{
    			return cost>b.cost;
    		}
    };
    int main(){ 
      	set<myComp> s;
      	myComp mycomp;
    	mycomp.cost=8;
      	s.insert(mycomp);
      	mycomp.cost=2;
      	s.insert(mycomp);
      	mycomp.cost=1;
      	s.insert(mycomp);
      	mycomp.cost=12;
      	s.insert(mycomp);
    
        set<myComp>::iterator it;
    	for(it=s.begin();it!=s.end();it++){
    		cout<<(*it).cost<<"\t";
    	}
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暴风雨中的白杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值