STL之set
来源:互联网 发布:亿维雅 知乎 编辑:程序博客网 时间:2024/05/29 03:08
一.常用用法:
s.begin() ,返回set容器的第一个元素
s.end() ,返回set容器的最后一个元素
s.erase() ,删除一个元素
s.clear() ,删除set容器中的所有的元素
s.empty() ,判断set容器是否为空
s.max_size() ,返回set容器可能包含的元素最大个数
s.size() ,返回当前set容器中的元素个数
s.rbegin() ,返回的值和end()相同
s.rend() ,返回的值和begin()相同
s.count() ,查找set中某个某个键值出现的次数。(判断某一键值是否出现过)
s.find() ,返回给定值值得定位器,如果没找到则返回end()。
#include<iostream>#include<set>using namespace std;int main(){ set<int> s;//定义一个集合为s s.insert(1);//插入 s.insert(2); s.insert(3); s.insert(1); cout<<"set 的 size 值为:"<<s.size()<<endl; cout<<"set 的包含的最大值为:"<<s.max_size()<<endl; cout<<"set 中第一个元素为:"<<*s.begin()<<endl; cout<<"set 中的最后一个元素是:"<<*s.end()<<endl; cout<<"set 中 1 出现的次数是 :"<<s.count(1)<<endl; cout<<"set 中 2 出现的次数是 :"<<s.count(2)<<endl; cout<<"set 中 4 出现的次数是 :"<<s.count(4)<<endl; s.clear();//清空 if(s.empty())//判断是否为空 cout<<"set 为空"<<endl; cout<<"set 的 size 值为:"<<s.size()<<endl; cout<<"set 的包含的最大值为:"<<s.max_size()<<endl; int a[] = {6,5,4,3,2,1}; set<int> ss(a,a+6); set<int>::iterator iter; if((iter = ss.find(2)) != ss.end()) { cout<<*iter<<endl; } return 0;}
结果
set 的 size 值为:3set 的包含的最大值为:214748364set 中第一个元素为:1set 中的最后一个元素是:3set 中 1 出现的次数是 :1set 中 2 出现的次数是 :1set 中 4 出现的次数是 :0set 为空set 的 size 值为:0set 的包含的最大值为:2147483642
二.
s.lower_bound() 返回第一个大于或等于给定关键值的元素
s.upper_bound()返回第一个大于给定关键值的元素
s.equal_range()返回一对定位器,分别表示 第一个大于或等于给定关键值的元素 和 第一个大于给定关键值的元素,这个返回值是一个pair类型,如果这一对定位器中哪个返回失败,就会等于s.end()
#include <iostream>#include <set>using namespace std;int main(){ set<int> s; s.insert(1); s.insert(2); s.insert(5); cout << "lower_bound & upper_bound test:" << endl; cout << "第一个大于或等于3的元素: " << *s.lower_bound(3) << endl; cout << "第一个大于或等于2的元素: " <<*s.lower_bound(2) << endl; cout << "第一个大于2的元素: " <<*s.upper_bound(2) << endl; cout << "equal_range test:" << endl; cout << "第一个大于或等于2的元素: " << *s.equal_range(2).first << endl; cout << "第一个大于2的元素: " << *s.equal_range(2).second << endl; return 0;}
结果
lower_bound & upper_bound test:第一个大于或等于3的元素: 5第一个大于或等于2的元素: 2第一个大于2的元素: 5equal_range test:第一个大于或等于2的元素: 2第一个大于2的元素: 5
三.自定义比较函数
(1)元素不是结构体:
自定义比较函数myCmp,重载“()”操作符
struct myCmp { bool operator()(const your_type &a,const your_type &b) { return a.data-b.data>0; } } set<int,myCmp>s; ...... set<int,myCmp>::iterator it;
(2)如果元素是结构体,可以直接将比较函数写在结构体内。
struct Info{ string name; float score; //重载“<”操作符,自定义排序规则 bool operator < (const Info &a) const { //按score从大到小排列 return a.score<score; }} set<Info> s;......set<Info>::iterator it;
后来补充:如何输出set内的内容
set<string>::iterator it; //定义迭代器for(it = s.begin();it != s.end();it++)cout<<*it<<endl;//输出it要加“*”
感谢耀辰大佬教我用迭代器。
阅读全文
0 0
- 【STL】STL之set
- stl之set
- STL 应用之set
- STL容器之set
- STL之<set>
- STL之set
- STL之set
- C++ STL之set
- C++ STL 之set
- STL 之 set 集合
- STL之set
- STL 之set 练习
- stl 之set图解
- STL之set姿势
- STL 之 set
- stl之set
- stl之hash set
- STL具体操作之set
- Lua math库
- 九 iOS 之CAAnimationGroup(动画组)
- 利用动态数组输入100~999内的水仙数数
- Window系统中安装Python和Django框架
- 第九周项目2
- STL之set
- windows环境mysql数据库的sql_mode配置
- linux基础命令--解压缩命令
- OpenStack双网卡、多节点搭建
- 使用MVP模式+Retrofit+Fresco加载网络数据
- mysql 安装
- Pat 1021. 个位数统计 (15)
- 分治算法---最大子数组
- luoguP3385 【模板】负环(dfs_Bellman)