[C++]-set 集合
2024-10-10 10:14:33
set集合用于存放一组无重复的元素。由于集合本身是有序的,所以可以高效地查找指定元素,也可以方便地得到指定大小范围地元素在容器中所处区间。
代码
#include<iostream>
#include<set>
using namespace std;
template<typename T>
inline void print_set(T s) // 定义该函数用于打印集合内容
{
for(typename T::iterator it = s.begin(); it!=s.end(); ++it)
cout<<*it<<" "; cout<<endl<<endl;
}
int main()
{
cout << "-----定义和初始化-----\n";
int a[] = {3,2,1,5,4};
// 定义一个集合,并初始化将数组a中的元素拷贝到集合中,默认是小于比较器less<int>(升序)
set<int> seta(a, a+5);
a[2] = 22; // 修改a数组中的元素值,集合seta不会发生改变
print_set(seta);
// 设置setb为大于比较器greater<int>(降序),初始化将seta的元素考北到setb中
set<int, greater<int> > setb(seta.begin(), seta.end());
print_set(setb);
cout << "----- 插入操作-----\n";
set<int> setc;
setc.insert(1); // insert()函数直接用于插入元素
setc.insert(++setc.begin(), 0); // 指定位置插入元素3
pair<set<int>::iterator, bool> r1 = setc.insert(22); // insert()函数返回值为二元组
pair<set<int>::iterator, bool> r2 = setc.insert(22); // 第二插入22不会成功
// 二元组第一个元素是插入位置的迭代器,第二个元素是插入成功与否的标志
// 成功插入返回true,失败返回false
cout << "r1:" << *r1.first << " " << r1.second << endl;
// 第二次插入22会失败,则r2.first就是集合中已存在的22的位置的迭代器
cout << "r2:" << *r2.first << " " << r2.second << endl;
int b[] = {5,6,7};
setc.insert(b+1, b+3); // b数组的后两个元素插入setc中
print_set(setc);
cout << "----- 删除操作-----\n";
int c[] = {1,2,3,4,5};
set<int> setd(c, c+5);
setd.erase(4); // 删除元素4
setd.erase(++setd.begin()); // 删除第二个元素
print_set(setd);
set<int> sete(c, c+5);
set<int>::iterator ita = sete.begin();
set<int>::iterator itb = --(--sete.end());
sete.erase(ita, itb); // 删除区间[ita, itb)之间的元素
print_set(sete);
sete.clear(); // 清空集合
if(sete.empty()) // 判空
cout << "集合已空" << endl << endl;
cout << "----- 查找操作-----\n";
int d[] = {1,2,3,4,5};
set<int> setf(d, d+5);
if(setf.find(3) != setf.end()) // 存在该元素,返回对应的迭代器
cout << "3在setf中" << " " << *setf.find(3) << endl;
if(setf.find(7) == setf.end()) // 不存在该元素,返回setf.end()
cout << "不存在该元素\n" << endl;
cout << "----- 基于键的查找-----\n";
int e[] = {2,3,1,5,4};
set<int> setg(e, e+5);
set<int>::iterator it1, it2;
it1 = setg.lower_bound(3); cout << *it1 <<endl; // 第一个键值不小于3的元素的迭代器
it2 = setg.upper_bound(3); cout << *it2 <<endl; // 第一个键值大于3的元素的迭代器
cout << setg.count(2) << " " << setg.count(9) << endl; // 获取集合中元素2和9的个数
pair<set<int>::iterator, set<int>::iterator> pa;
pa = setg.equal_range(2); // 获取一个区间,该区间(左闭右开)包含所有元素2
for(set<int>::iterator it = pa.first; it != pa.second; ++it)
cout << *it << " ";
}
运行结果
-----定义和初始化-----
1 2 3 4 5
5 4 3 2 1
----- 插入操作-----
r1:22 1
r2:22 0
0 1 6 7 22
----- 删除操作-----
1 3 5
4 5
集合已空
----- 查找操作-----
3在setf中 3
不存在该元素
----- 基于键的查找-----
3
4
1 0
2
感谢
最新文章
- RoundedBitmapDrawable生成圆角图片
- BeautifulSoup 的用法
- Java 字符串用逗号并接
- 使用main方法调用http请求本地服务器的某个servlet报错问题
- 使用MFC中的AfxBeginThread创建多线程
- 如何在Windows中打开多个Windows Media Player
- 简单的多表插入(oracle)
- 超过lua上帝的语言
- 如何做到 Laravel 配置可以网站后台配置【社交系统ThinkSNS+研发日记四】
- 201521123061 《Java程序设计》第七周学习总结
- 用node.js搭建本地服务器
- 一条shell命令让多台Linux服务器执行
- [LeetCode&;Python] Problem 27. Remove Element
- java百度云推送
- PostCSS理解与运用
- js随机生成颜色的方法
- spring mvc 提交表单汉字乱码
- 测试开发:Python+Django实现接口测试工具
- HDU 5306 Gorgeous Sequence[线段树区间最值操作]
- NS Simulation: Scheduling Events (examples inside)
热门文章
- SpringBoot | 1.2 全注解下的Spring IoC
- Spring:Spring项目多接口实现类报错找不到指定类
- [小技巧] gcc 编译选项-###
- mysql 索引介绍与运用
- 深入浅出图神经网络 第6章 GCN的性质 读书笔记
- 修改vcenter的Administrator@vsphere.local密码
- 三分钟掌握共享内存 &; Actor并发模型
- Redis的持久化机制你学会了吗
- 计算机毕业设计选题大合集,含ssm,springboot,小程序,php,python
- Spring总结之AOP