set容器底层使用红黑树实现,会自动将重复的数删除
使用时:
加入头文件#include
s.insert() 插入一个元素
s.erase() 删除一个元素
s.size() 返回当前set容器中的元素个数
s.begin() 返回set容器的第一个元素
s.end() 返回set容器的最后一个元素
s.clear() 删除set容器中的所有的元素
s.empty() 判断set容器是否为空
point.count(x) 判断x是否在集合之中,如果在集合之中则返回的值为1,如果不在集合之中返回的值为0
例如:
#include<iostream>
#include<set>
using namespace std;
int main()
{
set <int> a;
a.insert(1);
a.insert(2);
a.insert(1);
a.insert(1);
cout<<a.count(1)<<" "<<a.count(2)<<" "<<a.count(3)<<endl;
return 0;
}
输出结果
迭代器的使用(类似于for循环访问数组):
#include<iostream>
#include<set>
using namespace std;
int main()
{
set< int > temp;//定义一个点坐标的集合
temp.insert(1);temp.insert(2);temp.insert(3);temp.insert(4);temp.insert(5);
temp.insert(6);
set< int >::iterator t;
for(t=temp.begin();t!=temp.end();t++){//每一分钟每个黑点都要进行一次上下左右扩散
cout<<*t<<endl;
}
return 0;
}
}
输出结果: