学习笔记 | Set
目录
Set
前言
不会数据结构选手
当几乎没写过什么数据结构的菜鸡遇上了毒瘤的splay和treap 时间正一点一点地被续走TAT
听说set有时候可以替代treap和splay
那么菜鸡LLL就来学习一下set的神奇操作
简单的介绍==没介绍TAT
setmultisetmapmultimap等内部采用的就是一种非常高效的平衡检索二叉树
tips:
set中元素都是排好序的set中没有重复的元素(multiset可以有
set中常用的方法
begin() //返回set中第一个元素的迭代器
end() //返回指向当前set末尾元素的下一位置的迭代器
clear() //删除set容器里的所有元素
empty() //判断set容器是否为空
max_size() //返回set容器可能包含的元素个数
rbegin() //返回的值和end()相同
rend() //返回的值和begin()相同
count() //用来查找set中某个键值出现的次数
erase(iterator) //删除定位器iterator指向的值
erase(first,second) //删除定位器first和second之间的值
erase(key_value) // 删除键值key_value的值
equal_range() //返回一堆定位器 分别表示第一个大于或等于给定关键值的元素和第一个大于给定关键值的元素 这个返回值是一个pair类型 如果这一对定位器中哪个返回失败 就会等于end()值
find() //返回给定值的定位器 如果没有找到则返回end()
#include <iostream>
#include <set>
using namespace std;
int main(){
set<int> s;
set<int>::iterator iter;
for(int i = 1 ; i <= 5; ++i)
{
s.insert(i);
}
for(iter = s.begin() ; iter != s.end() ; ++iter)
{
cout<<*iter<<" ";
}
cout<<endl;
pair<set<int>::const_iterator,set<int>::const_iterator> pr;
pr = s.equal_range(3);
cout<<"第一个大于等于 3 的数是 :"<<*pr.first<<endl;
cout<<"第一个大于 3的数是 : "<<*pr.second<<endl;
return 0;
}
自定义比较函数
元素不是结构体
//自定义比较函数myComp 重载
()操作符struct myComp{ bool operator () (const your_type &a,const your type &b){ return a.data-b.data>0; } } set<int,myComp>s; set<int,myComp>:: iterator it;结构题
struct Info{ string name; double score; bool operator < (const Info &a) const{ return a.score < score; } } set<Info>s; set<Info>:: iterator it;
补充
lower_bound(elem) //返回元素值为elem的第一个可安插的位置 也就是元素值>=elem的第一个元素的位置
upper_bound(elem) //返回元素值为elem的最后一个可安插的位置 也就是元素值>elem的第一个元素位置
// cont/set1.cpp
#include <iostream>
#include <set>
using namespace std;
int main()
{
/*type of the collection:
*-no duplicates
*-elements are integral values
*-descending order
*/
typedef set<int,greater<int> > IntSet;
IntSet coll1; // empty set container
//insert elements in random order
coll1.insert(4);
coll1.insert(3);
coll1.insert(5);
coll1.insert(1);
coll1.insert(6);
coll1.insert(2);
coll1.insert(5);
//iterate over all elements and print them
IntSet::iterator pos;
for (pos = coll1.begin(); pos != coll1.end(); ++pos) {
cout << *pos << ' ';
}
cout << endl;
//insert 4 again and process return value
pair<IntSet::iterator,bool> status = coll1.insert(4);
if (status.second) {
cout << "4 inserted as element "
<< distance (coll1.begin(),status. first) + 1
<< endl;
}
else {
cout << "4 already exists" << endl;
}
//assign elements to another set with ascending order
set<int> coll2(coll1.begin(),
coll1.end());
//print all elements of the copy
copy (coll2.begin(), coll2.end(),
ostream_iterator<int>(cout," "));
cout << endl;
//remove all elements up to element with value 3
coll2.erase (coll2.begin(), coll2.find(3));
//remove all elements with value 5
int num;
num = coll2.erase (5);
cout << num << " element(s) removed" << endl;
//print all elements
copy (coll2.begin(), coll2.end(),
ostream_iterator<int>(cout," "));
cout << endl;
}
6 5 4 3 2 1
4 already exists
1 2 3 4 5 6
1 element(s) removed
3 4 6
// cont/mset1.cpp
#include <iostream>
#include <set>
using namespace std;
int main()
{
/*type of the collection:
*-duplicates allowed
*-elements are integral values
*-descending order
*/
typedef multiset<int,greater<int> > IntSet;
IntSet coll1, // empty multiset container
//insert elements in random order
coll1.insert(4);
coll1.insert(3);
coll1.insert(5);
coll1.insert(l);
coll1.insert(6);
coll1.insert(2);
coll1.insert(5);
//iterate over all elements and print them
IntSet::iterator pos;
for (pos = coll1.begin(); pos != coll1.end(); ++pos) {
cout << *pos << ' ';
}
cout << endl;
//insert 4 again and process return value
IntSet::iterator ipos = coll1.insert(4);
cout << "4 inserted as element "
<< distance (coll1.begin(),ipos) + 1
<< endl;
//assign elements to another multiset with ascending order
multiset<int> coll2(coll1.begin(),
coll1.end());
//print all elements of the copy
copy (coll2.begin(), coll2.end(),
ostream_iterator<int>(cout," "));
cout << endl;
//remove all elements up to element with value 3
coll2.erase (coll2.begin(), coll2.find(3));
//remove all elements with value 5
int num;
num = coll2.erase (5);
cout << num << " element(s) removed" << endl;
//print all elements
copy (coll2.begin(), coll2.end(),
ostream_iterator<int>(cout," "));
cout << endl;
}
6 5 5 4 3 2 1
4 inserted as element 5
1 2 3 4 4 5 5 6
2 element(s) removed
3 4 4 6
学习笔记 | Set的更多相关文章
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- seaJs学习笔记2 – seaJs组建库的使用
原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...
- CSS学习笔记
CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...
- HTML学习笔记
HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...
- DirectX Graphics Infrastructure(DXGI):最佳范例 学习笔记
今天要学习的这篇文章写的算是比较早的了,大概在DX11时代就写好了,当时龙书11版看得很潦草,并没有注意这篇文章,现在看12,觉得是跳不过去的一篇文章,地址如下: https://msdn.micro ...
- ucos实时操作系统学习笔记——任务间通信(消息)
ucos另一种任务间通信的机制是消息(mbox),个人感觉是它是queue中只有一个信息的特殊情况,从代码中可以很清楚的看到,因为之前有关于queue的学习笔记,所以一并讲一下mbox.为什么有了qu ...
随机推荐
- 【洛谷】【扩欧】P1516 青蛙的约会
[题目描述] 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有 ...
- Installing Oracle Database 12c Release 2(12.2) RAC on RHEL7.3 in Silent Mode
概要 在RHEL7静默方式安装oracle database 12.2 RAC. 一.环境配置 1. 配置hosts文件 cp /etc/hosts /etc/hosts_$(date +%Y%d%m ...
- Hive学习之路 (二)Hive安装
Hive的下载 下载地址http://mirrors.hust.edu.cn/apache/ 选择合适的Hive版本进行下载,进到stable-2文件夹可以看到稳定的2.x的版本是2.3.3 Hive ...
- c++——const关键字
1 const基础知识(用法.含义.好处) int main() { const int a; int const b; const int *c; int * const d; const int ...
- (转)一次压测对nginx/tomcat配置的调整
原文地址:还在寻找.... 一个web系统,前端使用nginx做为反向代理,处理https,并将请求转发给后端的tomcat服务. 压力测试工具选择了jmeter. 首先简单介绍一下jmeter. 它 ...
- 从CMDB查询云平台组件或者IP简单脚本
#!/bin/bash#author xiaoweige#todo: ip -- > ingredient or ingredient -- > ip #todo: get the ip ...
- ORA-00600: internal error code, arguments: [kcratr_nab_less_than_odr], [1], [1498], [18713], [18720]
数据库server出现ORA-00600[kcratr_nab_less_than_odr].不能open数据库 1.open数据库报ORA-00600[kcratr_nab_less_than_od ...
- helpera64开发板下制作ubuntu rootfs镜像
下一篇路径:https://www.cnblogs.com/jizizh/p/10499448.html 环境: HelperA64开发板 Linux3.10内核 时间:2019.02.14 目标:定 ...
- FPGA入门实例一:LFSR
一:任务: 要求使用Verilog语言在Xilinx Virtex-6开发板上实现线性反馈移位寄存器(LFSR)的硬件逻辑设计. 二:前期准备: 基本上完成一个简单的设计需要用到以下几个软件 逻辑:U ...
- 英语linux+英语firefox+英语Oracle OEM如何设置成显示日语
1 linux安装盘挂载,安装日语语言包 2 linux的系统语言设置为日语 3 firefox的 edit-> setting -> contents -> language se ...