STL之set容器用法
- set中每个元素都唯一,就像集合;
- set中每个元素都是排好序的,因为内部采用红黑树实现;
- set区别与vector,它不能通过下标访问;
1.0 头文件
#include <set>
1.1 创建set
set<int> S; //声明一个int型vector
set<int> S(10); //初始大小为10
1.2 基本用法
S.size() //集合大小
S.insert(x) //插入元素x
S.erase(x) //删除元素x
S.clear() //清空集合
S.count(x) //集合中x元素的
1.3 set容器的遍历
//采用iterator迭代
set<int>::iterator it;
for (it=S.begin(); it!=S.end(); ++it)
cout << ' ' << *it;
//使用auto
for (auto s:S)
cout << s;
来源:CSDN
作者:鱼日天
链接:https://blog.csdn.net/luhao19980909/article/details/89913486