目录
【说明】如果这篇文章对你有用的话,动动手点个赞吧 (.^^.)
一: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<<" ";
}
}
六:总结
其实这个容器的主要用途就是 “去重 ”,对于某些问题的解决,会起到较大的作用,所以还是有必要去掌握一下的。