set 的详细用法(set 排序、set 的遍历、set 的多种倒序遍历方法、set 的基本成员函数)

目录

一:set 的简介

二:set的使用(要包含头文件)

1. set 的定义

2.set 的基本成员函数

3.set 的遍历

(1)迭代器 iterator(即升序输出)

(2)倒序输出

1. rbegin()和 rend()

2.当然,也可以逆向思维一下。

^^3. 用 greater 实现降序排列

三:应用基本成员函数的代码

【总结】有上述代码可以看出,插入的数据是无序的,且包含重复的数据,但输出结果是有序的,由小到大依次输出,且不包含重复的,直观说明了 set 容器 “去重” 的特点,且说明了 set 容器是默认数据由低到高即升序来排列。

四:set 排序

这里主要讲存储结构体时该如何排序:要重载运算符

五:简单例题

六:总结


【说明】如果这篇文章对你有用的话,动动手点个赞吧 (.^^.)

一:set 的简介

set 就是集合的意思,而集合的特点就是不会出现重复的内容,这也就是 set 容器存储数据的特点,即去重

二:set的使用(要包含头文件<map>)

1. set 的定义
set<存储的类型> mySet; 
2.set 的基本成员函数

insert()//插入元素
count()//count()计数,但 set 中没有重复元素,所以只能返回0或 1,所以可以用来判断容器中是否存在某个元素,若有返回1,否则0
size()//返回容器的尺寸,也可以是元素的个数
erase()//删除容器中某个元素
clear()//清空容器中的元素
empty()//判断容器是否为空 ,若是,返回 1,否则,返回 0
begin()//返回第一个节点的迭代器
end()//返回最后一个节点加 1 的迭代器 
rbegin()//反向迭代器 
rend()//反向迭代器

3.set 的遍历
(1)迭代器 iterator(即升序输出)
#include <iostream>
#include <set> 
#include <string>
using namespace std;
int main()
{
	set<int> mySet;
	// 
	mySet.insert(1);
	mySet.insert(3);
	mySet.insert(2);
    // 
    set<int>::iterator it;//使用迭代器 
	for(it=mySet.begin();it!=mySet.end();it++)
	cout<<*it<<" "; 
	
}

(2)倒序输出
1. rbegin()和 rend()

需要使用反向迭代器 reverse_iterator it

#include <iostream>
#include <set> 
using namespace std;
int main()
{
	set<int> mySet;
	// 
	mySet.insert(1);
	mySet.insert(3);
	mySet.insert(2);
    // 
    set<int>::reverse_iterator it;//使用反向迭代器 
	for(it=mySet.rbegin();it!=mySet.rend();it++)
	cout<<*it<<" "; 
	
}

2.当然,也可以逆向思维一下。
#include <iostream>
#include <set> 
using namespace std;
int main()
{
	set<int> mySet;
	// 
	mySet.insert(1);
	mySet.insert(3);
	mySet.insert(2);
    // 
    set<int>::iterator it;//使用迭代器 
	for(it=--mySet.end();it!=--mySet.begin();it--)
	cout<<*it<<" "; 
	
}

^^3. 用 greater<int> 实现降序排列

#include <iostream>
#include <set>
using namespace std;
int main()
{
	//
    set<int,greater<int> > s;
		
	s.insert(21);
	s.insert(13);
	s.insert(54);
	s.insert(46);
	
	set<int>::iterator i;
	for(i=s.begin();i!=s.end();i++)
	cout<<*i<<" ";

}

三:应用基本成员函数的代码

代码如下:

#include <iostream>
#include <set> 
#include <string>
using namespace std;
int main()
{
	set<int> mySet;
	set<int>::iterator it;
	 
	mySet.insert(42);
	mySet.insert(13);
	mySet.insert(21);
	mySet.insert(41);
	mySet.insert(54);
	mySet.insert(34);
	mySet.insert(43);
	mySet.insert(41);
	
	cout<<"插入后的数据为"<<endl;
	for(it=mySet.begin();it!=mySet.end();it++)
	cout<<*it<<" ";
	cout<<endl;
//----------------------------------------------------------------	
	mySet.erase(41);
//当然,也可以通过迭代器删除指定位置的,mySet.erase(mySet.begin())
	cout<<"删除后的数据为"<<endl;
	for(it=mySet.begin();it!=mySet.end();it++)
	cout<<*it<<" ";
	cout<<endl;
//----------------------------------------------------------------- 
	cout<<"是否包含元素13:"; 
	if(mySet.count(13)==1)cout<<"包含"<<endl;
	else cout<<"不包含"<<endl;
	cout<<"是否包含元素60:"; 
	if(mySet.count(60)==1)cout<<"包含"<<endl;
	else cout<<"不包含"<<endl; 
//------------------------------------------------------------
	cout<<"元素个数为:"<<mySet.size()<<"个"<<endl;
//--------------------------------------------------------------------
    cout<<"容器是否为空:";
    if(mySet.empty())cout<<"是"<<endl;
    else cout<<"否"<<endl;
//----------------------------------------------------------------
    mySet.clear();
	cout<<"清除后容器还剩的元素数为:"<<mySet.size()<<"个"<<endl; 
}

【总结】有上述代码可以看出,插入的数据是无序的,且包含重复的数据,但输出结果是有序的,由小到大依次输出,且不包含重复的,直观说明了 set 容器 “去重” 的特点,且说明了 set 容器是默认数据由低到高即升序来排列。

四:set 排序

这里主要讲存储结构体时该如何排序:要重载运算符

代码如下:

#include <iostream>
#include <set>
using namespace std;
struct Student
{
    string name;
	int score;
//重载运算符
	bool operator<(const Student &s)const
	{
	    if(score!=s.score)
		return score>s.score;
		else
		return name>s.name;	
	}	
};
int main()
{
	//
    set<Student> s;
	Student t;
	t.name="jfiu";t.score=32;s.insert(t);
	t.name="koru";t.score=13;s.insert(t);
	t.name="kaer";t.score=54;s.insert(t);
	t.name="opwq";t.score=46;s.insert(t);
	t.name="refq";t.score=54;s.insert(t);
	
	set<Student>::iterator it;
	for(it=s.begin();it!=s.end();it++)
	cout<<(*it).score<<" "<<(*it).name<<" "<<endl;

} 

五:简单例题

【问题描述】

输入n个整数,找出其中最小的k(k<=n)个不同数。例如输入4,5,1,6,1,7,3,8这8个数字,则最小的4个数字是1,3,4,5。

【输入形式】

每个测试案例包括2行:

第一行为2个整数n,k(1<=n,k<=200000),表示数组的长度。

第二行包含n个整数,表示这n个数,数组中的数的范围是[0,1000 000 000]。

【输出形式】

对应每个测试案例,输出最小的k个数,并按从小到大顺序打印(如果不存在k个不同的数,则按照实际数量进行输出)。

【样例输入】

8 4
4 5 1 6 2 7 3 8

【样例输出】

1 2 3 4

分析:这个题就明显用到 set 容器(既不用额外排序,也不用考虑去重),当然其他方法也可以,但会麻烦很多,高下立见。

代码如下:

#include <iostream>
#include <set>
using namespace std;
int main()
{
	set<int> mySet;
	int n,k;
	cin>>n>>k;
	int x;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		mySet.insert(x);
	}
	set<int>::iterator it;
	int a=0;
	for(it=mySet.begin();it!=mySet.end();it++)
	{
		if(a==k)break;
		a++;
		cout<<*it<<" ";
	} 
	
}

六:总结

其实这个容器的主要用途就是 “去重 ”,对于某些问题的解决,会起到较大的作用,所以还是有必要去掌握一下的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

.jc7

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

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

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

打赏作者

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

抵扣说明:

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

余额充值