C++STL容器——set
set 介绍set 正如它的名字一样,与数学中的集合是相似的,也具有数学中集合的三个特征,即确定性(集合中的元素必须是确定的)。互异性(集合中的元素互不相同)。例如:集合A={1,a},则a不能等于1 。无序性(集合中的元素没有先后之分),如集合{3,4,5}和{3,5,4}算作同一个集合。所以,set 中的元素默认按升序排列且不存在重复元素,可使用它完成排序与去重的任务。set 基本操作头文件:
set 介绍
set 正如它的名字一样,与数学中的集合是相似的,也具有数学中集合的三个特征,即
- 确定性(集合中的元素必须是确定的)。
- 互异性(集合中的元素互不相同)。例如:集合A={1,a},则a不能等于1 。
- 无序性(集合中的元素没有先后之分),如集合{3,4,5}和{3,5,4}算作同一个集合。
所以,set 中的元素默认按升序排列且不存在重复元素,可使用它完成排序与去重的任务。
set 基本操作
头文件:
#include<set>
定义:
集合类型的定义格式:
set<集合元素类型>集合命称;
例如:一个元素都是整形的S集合
set<int>S;
基本操作:
成员函数:
构造函数(constructor):
参数 |
set&& __x |
initializer_list<value_type> __l, const _Compare& __comp = _Compare (), const allocator_type& __a = allocator_type() |
const allocator_type& __a |
_InputIerator __first, _InputIerator __last |
_InputIerator __first, _InputIerator __last, const _Compare& __comp = _Compare (), const allocator_type& __a = allocator_type() |
const set& __x |
_InputIerator __first, _InputIerator __last, const allocator_type& __a |
const _Compare& __comp = _Compare (), const allocator_type& __a = allocator_type() |
void |
const set&& __x, const allocator_type& __a = allocator_type() |
set&& __x, const allocator_type& __a = allocator_type() |
initializer_list<value_type> __l, const allocator_type& __a = allocator_type() |
例:(运用构造函数 ( set(const set& __x) ) )
#include<stdio.h>
#include<set>
using std::set;
set<int> S;
int main(void){
int n;
scanf("%d",&n);
for(int i=0,temp;i<n;i++){
scanf("%d",&temp);
S.insert(temp);
}
set<int> A(S);
for(set<int>::iterator iter=A.begin(),end=A.end();iter!=end;iter++)
printf("%d ",*iter);
return 0;
}
输入数据:
5
1 2 3 4 5
输出结果:
1 2 3 4 5
公有函数:
下面以一个集合S为例,进行说明。
函数名 | 功能、作用 |
S.begin(); | 返回S的第一个元素的地址 |
S.end(); | 返回S的最后一个元素的地址+1 |
S.clear(); | 清空集合S |
S.empty(); | 返回一个 bool 值,若集合S为空则返回 true ,不为空返回 false |
S.size(); | 返回集合S中元素的个数 |
S.erase(it); | 删除指针it所指向的集合元素 |
S.insert(n); | 在集合S中插入 n 这个元素 |
S.count(n); | 返回 n 在集合S中的出现次数 |
S.find(n); | 返回 n 在集合S中出现的位置,若 n 在集合中,返回指向 n 的迭代器,否则返回 S.end(); |
迭代器:
集合并不能像数组那样通过下标访问元素(这与它本身的实现方式有关),为了访问集合中的元素,我们需要使用 STL 提供的迭代器来对集合元素进行访问。
迭代器的定义:
set<集合元素类型>::iterator 迭代器名称;
迭代器的运用实例:
#include<stdio.h>
#include<set>
using std::set;
int main(void){
set<int>S;
for(int i=1;i<=5;i++)
S.insert(i);//在集合中插入元素 1,2,3,4,5
for(set<int>::iterator it=S.begin()/*定义迭代器 it 并赋值*/;it!=S.end();it++)
printf("%d ",*it);//*it访问迭代器所指向的元素
return 0;
}
程序运行结果:
1 2 3 4 5
set 使用实例:
NOIP 2006 普及组 第一题 明明的随机数:
题目给出N个随机数,要求你将这些数从小到大排序并去重。
输入格式:
输入有2行,第1行为1个正整数,表示所生成的随机数的个数。第2行有N个用空格隔开的正整数,为所产生的随机数。
输出格式:输出有2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。
我们可以利用集合默认按升序排列且不存在重复元素的性质,迅速地完成这题。
#include<stdio.h>
#include<set>
using std::set;
set<int> S;
int read(int &x){
int f=1;char ch;x=0;
do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||'9'<ch);
while('0'<=ch&&ch<='9'){x=10*x+ch-'0';ch=getchar();}x*=f;
return x;
}//读入优化
int main(void){
int n;
read(n);
for(int i=0,temp;i<n;i++)
read(temp),S.insert(temp);
printf("%d\n",S.size());
for(set<int>::iterator p=S.begin();p!=S.end();p++)
printf("%d ",*p);
return 0;
}
提示:
当集合的元素类型为结构体时,因为集合元素要进行大小比较,所以必须对 运算号 '<' 进行重载。
更多推荐
所有评论(0)