学习笔记 | 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 ...
随机推荐
- Jsp实现在线作业提交系统
Jsp实现在线作业提交系统 作为 Computer Science 的学生,凌晨四点之前睡都应该感到羞耻. 项目托管地址:https://github.com/four-in-the-morning/ ...
- Python基本知识3----序列
前言: 序列:列表/元组/字符串 3种序列的共同点: 都可以通过索引得到每一个元素 默认索引值从0开始(还支持负数) 都可以通过切片的方式得到范围内的元素的集合 有很多共同的操作符(重复操作符.拼接操 ...
- CentOS中用Nexus搭建maven私服,为Hadoop编译提供本地镜像
系统: CentOS release 6.6 (Final) Nexus:nexus-2.8.1-bundle.tar.gz,下载地址:https://sonatype-download.global ...
- POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48380 Accepted: 18252 ...
- HDU 1203 01背包变形题,(新思路)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1203 I NEED A OFFER! Time Limit: 2000/1000 MS (Java/ ...
- STM32F103 ucLinux内核没有完全启动
STM32F103 ucLinux内核没有完全启动 从BOOT跳转到内核后,执行一长段的汇编语言,然后来到startkernel函数,开启C语言之旅. 但是内核输出不正常,如下所示: Linux ve ...
- 【星云测试】Devops微服务架构下具有代码级穿透能力的精准测试
微服务是Devops场景下热门的开发框架,在大型项目中被广泛采用.它把一个大型的单个应用程序和服务拆分为数十个的支持微服务,独立部署.互相隔离,通过扩展组件来处理功能瓶颈问题,比传统的应用程序更能有效 ...
- java 快速开发框架平台 二次开发 代码生成器 springmvc SSM后台框架源码
官网 http://www.fhadmin.org/D 集成安全权限框架shiro Shiro 是一个用 Java 语言实现的框架,通过一个简单易用的 API 提供身份验证和授权,更安全,更可靠E ...
- mac 设置mysql开机自启动
1.编辑一个mysql启动文件. 在终端里面输入: sudo vi /Library/LaunchDaemons/com.mysql.mysql.plist 2.输入启动文件内容: <?xml ...
- Delphi的idhttp报IOHandler value is not valid错误的原因
出现这种问题的原因是由于访问的 URL地址为https或存在其跳转地址为https. 首先单纯使用idhttp是只能访问http,而https则需要搭配IdSSLIOHandlerSocketOpen ...