关于set

set是以特定的顺序存储相异元素的容器。

set是关联式容器,C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Red-Black Tree)。RB树的统计性能要好于一般平衡二叉树,所以被STL选择作为了关联容器的内部结构。

查找、插入、删除的时间复杂度都是O(logn)

一些特点:

1、在插入或赋初值时会自动排序(除非是unordered_set)

2、不能直接改变元素值,因为那样会打乱原本正确的顺序,要改变元素值必须先删除旧元素,则插入新元素

3、不提供直接存取元素的任何操作函数,只能通过迭代器进行间接存取,而且从迭代器角度来看,元素值是常数

4、每次insert或者erase之后,以前保存的iterator不会失效(关联式容器,只改变了一些指针的指向,原指针指向的内存没变)(当然,直接erase保存的指针肯定改变了)

set中的模板函数

begin()    返回指向第一个元素的迭代器

end()       返回指向最后一个元素的后一个位置的迭代器

clear()    清空内容

size()     返回当前元素个数

count(key_value)  用来查找set中某个键值出现的次数,在set只可能为0或1

erase(key_value)       删除键值为key_value的元素

erase(iterator)            删除迭代器iterator指向的元素

erase(first,second)    删除迭代器first和second之间的元素,[first,second)

find(key_value)          返回指向key_value的迭代器,如果没有找到返回end()

insert(key_value)      将key_value插入到set中

lower_bound(key_value)    返回第一个大于等于key_value的迭代器

upper_bound(key_value)   返回第一个大于key_value的迭代器

equal_range(key_value)     返回一对迭代器,first等同于lower_bound,second_bound等同于upper_bound,即 [lower_bound,upper_bound)

//若set<int>a,b; vector<int>c

set_union(a.begin(),a.end(),b.begin(),b.end(),back_insert(c))                        并集

set_intersection(a.begin(),a.end(),b.begin(),b.end(),back_insert(c))        交集

set_difference(a.begin(),a.end(),b.begin(),b.end(),back_insert(c))          差集

set_symmetric_difference(a.begin(),a.end(),b.begin(),b.end(),back_insert(c)) 对称差

//使用实例

//初始化set

//这个参考资料好少啊,自己尝试了这几种

 #include<cstdio>
#include<set>
using namespace std; int main()
{
int arr[] = { ,,,, }; //定义时赋初值
set<int>st1{,,,,};
set<int>st2 = { ,,,, };
set<int>st3(arr, arr + ); //先定义,后赋值
set<int>st4;
st4 = { ,,,, };
set<int>st5;
st5.insert(arr, arr + ); return ;
}

//基本操作

 #include<cstdio>
#include<set>
using namespace std; int main()
{
int arr[] = { , , , , , , , };
set<int>st(arr,arr + ); //遍历
//不像vector,只能通过迭代器进行间接存取
for (set<int>::iterator it = st.begin(); it != st.end(); it++)
printf("%d ", *it);
printf("\n"); //查找
//未找到返回end()
set<int>::iterator it = st.find();
if(it != st.end()) printf("%d\n", *it); //插入
//返回值为pair<set<int>::iterator,bool>
st.insert(); st.insert(); //删除
//不要去删除不存在的元素
st.erase(); st.erase(); //计数,是否在集合中
if (st.count()) printf("In\n");
else printf("Out\n"); //上下界函数
set<int>::iterator it1 = st.lower_bound();
set<int>::iterator it2 = st.upper_bound(); //区间定位
pair<set<int>::const_iterator, set<int>::const_iterator>pr;
pr = st.equal_range();
if(pr.second != st.end()) printf("%d %d\n", *pr.first, *pr.second); for (set<int>::iterator it = st.begin(); it != st.end(); it++)
printf("%d ", *it);
return ;
}

//集合操作

 #include<cstdio>
#include<set>
#include<vector>
#include<iterator> //inserter函数定义在里面
#include<algorithm> //set_union,set_intersection等定义在里面
using namespace std; int main()
{
//若待处理的集合用vecter保存,必须确保无重复且有序
//若用vector保存结果,使用函数back_inserter(dest)
vector<int>v1 = { ,,,,, };
vector<int>v2 = { ,,,,,, };
vector<int>dest1;
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(dest1));
//set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(dest));
//set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(dest));
//set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(dest)); //若用set保存结果,使用函数inserter(dest,dest.begin())
set<int>dest2;
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(dest2, dest2.begin())); //若待处理的集合用set保存,可以无序重复(会自动去重、排序)
set<int>s1 = { ,,,,,,,, };
set<int>s2 = { ,,,,,, };
set<int>dest3;
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(dest3, dest3.begin())); return ;
}

参考资料:

1、http://www.cplusplus.com/reference/set/set/?kw=set

2、https://blog.csdn.net/changjiale110/article/details/79108447

3、https://blog.csdn.net/rocky_56X/article/details/81772646

4、https://blog.csdn.net/yang20141109/article/details/51782027

STL:set的使用的更多相关文章

  1. 详细解说 STL 排序(Sort)

    0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...

  2. STL标准模板库(简介)

    标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...

  3. STL的std::find和std::find_if

    std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...

  4. STL: unordered_map 自定义键值使用

    使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...

  5. C++ STL简述

    前言 最近要找工作,免不得要有一番笔试,今年好像突然就都流行在线笔试了,真是搞的我一塌糊涂.有的公司呢,不支持Python,Java我也不会,C有些数据结构又有些复杂,所以是时候把STL再看一遍了-不 ...

  6. codevs 1285 二叉查找树STL基本用法

    C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...

  7. STL bind1st bind2nd详解

    STL bind1st bind2nd详解   先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...

  8. STL sort 函数实现详解

    作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不 ...

  9. STL的使用

    Vector:不定长数组 Vector是C++里的不定长数组,相比传统数组vector主要更灵活,便于节省空间,邻接表的实现等.而且它在STL中时间效率也很高效:几乎与数组不相上下. #include ...

  10. [C/C++] C/C++延伸学习系列之STL及Boost库概述

    想要彻底搞懂C++是很难的,或许是不太现实的.但是不积硅步,无以至千里,所以抽时间来坚持学习一点,总结一点,多多锻炼几次,相信总有一天我们会变得"了解"C++. 1. C++标准库 ...

随机推荐

  1. 迭代器-iteration

    class CoffrrIterator implements Iterator<Coffee> { int cunt = size; public boolean hasNext() { ...

  2. HDU2255 奔小康赚大钱 —— 二分图最大权匹配 KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    ...

  3. Ubuntu linux 返回上一次访问的目录

    cd - (cd空格 减号)返回最近一次访问的目录 这个非常方便.平时经常用终端切换目录,能够方便地回到原来的目录就很爽了. jiqing@jiqing-pad:/usr/local/redis/sr ...

  4. 运算符:三目运算符,运算符优先级,sizeof,自增自减,取余

    一://---------运算符-----------// 1.运算符是告诉编译程序执行特定算术或逻辑操作的符号. 2.按照功能划分: 算术运算符. 关系运算符与逻辑运算符.按位运算符. 3.运算符根 ...

  5. bzoj4196 [Noi2015]软件包管理器——树链剖分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4196 树链剖分. 代码如下: #include<iostream> #inclu ...

  6. go语言godep使用命令

    godep 看见他的star比govendor,所以我使用它.官方地址 https://github.com/tools/godep install   1 go get github.com/too ...

  7. Gym 100512B Betting Fast (题意+概率)

    题意:你开始有 s 元钱,然后你要在 t 场内赚到 n 元,每次赢的概率是 p,并且要越快越好. 析:当时没注意这个条件,要越快越好,然后写概率dp,怎么看也不像是对.其实是每次赌 min(s, n- ...

  8. PostgreSQL完整备份与还原过程

    1. 备份10.12.2.100PC机(服务器)上的数据库(仅备份数据库和对应的数据库里面各表的结构): pg_dump -h 10.12.2.100 -U postgres -p 8101 -d t ...

  9. bzoj 2561: 最小生成树【最小割】

    看错题了以为多组询问吓得不行-- 其实还挺好想的,就是数据范围一点都不网络流.把U作为s,V作为t,以最小生成树为例,(U,V,L)要在最小生成树上,就要求所有边权比L小的边不能连通(U,V)所在的联 ...

  10. 安装使用electron辛路历程

    安装使用electron辛路历程 成功安装electron以及成功使用第一个应用,整整花费了我一整天的时间,各种百度,各种尝试.最终,终于总结了一个亲测可行的终极可执行方案: electron 简单介 ...