C++ STL容器(三)之set

C++ STL中的set是一个有序关联的集合容器,使用红黑树(Red-Black Tree,平衡二叉检索树)这种数据结构组织泛化的元素数据。
由于红黑树的良好性质,其中元素检索的时间复杂度为logn(其中n为元素个数)
使用时需用宏语句:

#include<set>

一.几种创建set的方法:

    1.用默认比较函数对象(默认元素从小到大)来创建空的set;
	set<int>s1;
	
	2.指定一个比较函数对象来创建set对象; 
	struct comp{
	    bool operator()(const int& a,const int& b)
	    {
	    	return a>b;
	    }
    };//这个要放到main函数外独立存在;
	set<int,comp>s2;
	
	3.拷贝另一个set中元素;
	set<int>s3(s2);
	
	4.赋值迭代器区间[first,last)中的元素建立set;
	int a[]={1,2,3,4,5};
	set<int>s4(a,a+5);
	
	5.迭代器区间赋值+重写比较函数对象;
	struct comp{
        bool operator()(const char* s1,const char* s2)const
	    {
	    	return strcmp(s1,s2)<0;
     	}
    };
	const char* str[]={"hello","language","c++"};
	set<const char*,comp>s(str,str+3); 

二.插入/删除/检索元素

    set<int>s;
	
	插入元素value(若集合中已有元素则插入失败)
	s.insert(1);
	s.insert(2);
	s.insert(3);
	
	将迭代器[first,last)区间中的元素数据插入到set中;
	int a[]={4,5,6,7,8};
	s.insert(a,a+5);
    
    删除值为value的元素(或删除迭代器中[first,last)的所有元素)
	s.erase(1);
	
	搜索元素:返回元素在迭代器中的位置,若不存在该元素值则返回end
	if(s.find(1)!=s.end()) cout<<"元素1在set中"<<endl;
	else cout<<"元素1不在set中"<<endl;

三.迭代器遍历(正向/反向):

    //迭代遍历(正向/逆向)
    1.正向迭代遍历:
	set<int>::iterator i;
	cout<<"正向迭代遍历:";
	for(i=s.begin();i!=s.end();i++)
	{
		cout<<*i<<" ";
	}
	cout<<endl;
	
	2.逆向迭代遍历:
	set<int>::reverse_iterator ri;
	cout<<"逆向迭代遍历:";
	for(ri=s.rbegin();ri!=s.rend();ri++)
	{
		cout<<*ri<<" ";
	}
	cout<<endl;

四.其他常用功能:

    1.返回元素个数;
	cout<<"set中的元素个数为:"<<s.size()<<endl;
	
	2.清空set;
	s.clear();
	
    3.判断set是否为空;
	if(s.empty()) cout<<"set为空"<<endl; 

五.测试代码:

#include<cstdio>
#include<set>
#include<iostream>
using namespace std;

int main()
{
	/*几种创建set的方法; 
	//1.用默认比较函数对象(默认元素从小到大)来创建空的set;
	set<int>s1;
	
	//2.指定一个比较函数对象来创建set对象; 
	struct comp{
	    bool operator()(const int& a,const int& b)
	    {
	    	return a>b;
	    }
    };//这个要放到main函数外独立存在;
	set<int,comp>s2;
	
	//3.拷贝另一个set中元素;
	set<int>s3(s2);
	
	//4.赋值迭代器区间[first,last)中的元素建立set;
	int a[]={1,2,3,4,5};
	set<int>s4(a,a+5);
	
	//5.迭代器区间赋值+重写比较函数对象;
	struct comp{
        bool operator()(const char* s1,const char* s2)const
	    {
	    	return strcmp(s1,s2)<0;
     	}
    };
	const char* str[]={"hello","language","c++"};
	set<const char*,comp>s(str,str+3); 
	*/
	
	set<int>s;
	
	//插入元素value(若集合中已有元素则插入失败)
	s.insert(1);
	s.insert(2);
	s.insert(3);
	
	//将迭代器[first,last)区间中的元素数据插入到set中;
	int a[]={4,5,6,7,8};
	s.insert(a,a+5);
	
	//迭代遍历(正向/逆向)
	set<int>::iterator i;
	cout<<"正向迭代遍历:";
	for(i=s.begin();i!=s.end();i++)
	{
		cout<<*i<<" ";
	}
	cout<<endl;
	
	//逆向迭代遍历:
	set<int>::reverse_iterator ri;
	cout<<"逆向迭代遍历:";
	for(ri=s.rbegin();ri!=s.rend();ri++)
	{
		cout<<*ri<<" ";
	}
	cout<<endl;
	
	//删除值为value的元素(或删除迭代器中[first,last)的所有元素)
	s.erase(1);
	cout<<"删除元素1后:"; 
	for(i=s.begin();i!=s.end();i++)
	{
		cout<<*i<<" ";
	}
	cout<<endl; 
    
    //返回元素个数;
	cout<<"set中的元素个数为:"<<s.size()<<endl; 
    
    //搜索元素:返回元素在迭代器中的位置,若不存在该元素值则返回end
	if(s.find(1)!=s.end()) cout<<"元素1在set中"<<endl;
	else cout<<"元素1不在set中"<<endl;
	 
	if(s.find(6)!=s.end()) cout<<"元素6在set中"<<endl;
	else cout<<"元素6不在set中"<<endl;
	
	//清空set
	s.clear();
	
	//判断set是否为空;
	cout<<"清空set后:";
	if(s.empty()) cout<<"set为空"<<endl; 
	
	return 0;
 } 

测试结果:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值