stl 之set图解
Set、multiset都是集合类,区别在与set中不同意有反复元素,multiset中同意有反复元素。

sets和multiset内部以平衡二叉树实现

1. 经常使用函数
1) 构造函数和析构函数
set c:创建空集合,不包括不论什么元素
set c(op):以op为排序准则,产生一个空的set
set c1(c2):复制c2中的元素到c1中
set c(const value_type *first, const value_type* last):复制[first, last)之间元素构成新集合
set c(const value_type *first, const value_type* last,op):以op为排序准则,复制[first, last)之间元素构成新集合。
c.~set()销毁全部元素,释放内存
multiset mc:创建空集合,不包括不论什么元素
multiset mc(op):以op为排序准则。产生一个空的set
multiset c1(c2):复制c2中的元素到c1中
multiset c(const value_type *first, const value_type* last):复制[first, last)之间元素构成新集合
multiset c(const value_type *first, const value_type* last,op):以op为排序准则,复制[first, last)之间元素构成新集合。
c.~set()销毁全部元素,释放内存
- // constructing sets
- #include <iostream>
- #include <set>
- bool fncomp (int lhs, int rhs) {return lhs<rhs;}
- struct classcomp {
- bool operator() (const int& lhs, const int& rhs) const
- {return lhs<rhs;}
- };
- int main ()
- {
- std::set<int> first; // empty set of ints
- int myints[]= {10,20,30,40,50};
- std::set<int> second (myints,myints+5); // range
- std::set<int> third (second); // a copy of second
- std::set<int> fourth (second.begin(), second.end()); // iterator ctor.
- std::set<int,classcomp> fifth; // class as Compare
- bool(*fn_pt)(int,int) = fncomp;
- std::set<int,bool(*)(int,int)> sixth (fn_pt); // function pointer as Compare
- return 0;
- }
2) 大小、推断空函数
int size() const:返回容器元素个数
bool empty() const:推断容器是否为空,若返回true,表明容器已空
3) 添加、删除函数
pair<iterator,bool> insert( x):插入元素x
iterator insert(iterator it,x):在迭代器it处插入元素x
void insert(const value_type *first,const value_type *last):插入[first, last)之间元素
iterator erase(iterator it):删除迭代器指针it处元素
iterator erase(iterator first,iterator last):删除[first, last)之间元素
size_type erase(const Key& key):删除元素值等于key的元素
- #include <iostream>
- #include <set>
- int main ()
- {
- std::set<int> myset;
- std::set<int>::iterator it;
- std::pair<std::set<int>::iterator,bool> ret;
- // set some initial values:
- for (int i=1; i<=5; ++i) myset.insert(i*10); // set: 10 20 30 40 50
- ret = myset.insert(20); // no new element inserted
- if (ret.second==false) it=ret.first; // "it" now points to element 20
- myset.insert (it,25); // max efficiency inserting
- myset.insert (it,24); // max efficiency inserting
- myset.insert (it,26); // no max efficiency inserting
- int myints[]= {5,10,15}; // 10 already in set, not inserted
- myset.insert (myints,myints+3);
- std::cout << "myset contains:";
- for (it=myset.begin(); it!=myset.end(); ++it)
- std::cout << ' ' << *it;
- std::cout << '\n';
- return 0;
- }
- #include <iostream>
- #include <set>
- int main ()
- {
- std::set<int> myset;
- std::set<int>::iterator it;
- // insert some values:
- for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
- it = myset.begin();
- ++it; // "it" points now to 20
- myset.erase (it);
- myset.erase (40);
- it = myset.find (60);
- myset.erase (it, myset.end());
- std::cout << "myset contains:";
- for (it=myset.begin(); it!=myset.end(); ++it)
- std::cout << ' ' << *it;
- std::cout << '\n';
- return 0;
- }
4) 遍历函数
iterator begin():返回首元素的迭代器指针
iterator end():返回尾元素的迭代器指针
reverse_iterator rbegin():返回尾元素的逆向迭代器指针
reverse_iterator rend():返回首元素前一个位置的迭代器指针
- #include <iostream>
- #include <set>
- int main ()
- {
- int myints[] = {75,23,65,42,13};
- std::set<int> myset (myints,myints+5);
- std::cout << "myset contains:";
- for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
- std::cout << ' ' << *it;
- std::cout << '\n';
- return 0;
- }
5) 操作函数
const_iterator lower_bound(const Key& key):返回容器中大于等于key的迭代器指针
const_iterator upper_bound(const Key& key):返回容器中大于key的迭代器指针
int count(const Key& key) const:返回容器中元素等于key的元素的个数
pair<const_iterator,const_iterator> equal_range(const Key& key) const:返回容器中元素值等于key的迭代指针[first, last)
const_iterator find(const Key& key) const:查找功能,返回元素值等于key的迭代器指针
void swap(set& s):交换集合元素
void swap(multiset& s):交换多集合元素
- #include <iostream>
- #include <set>
- int main ()
- {
- std::set<int> myset;
- std::set<int>::iterator itlow,itup;
- for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
- itlow=myset.lower_bound (30); // ^
- itup=myset.upper_bound (60); // ^
- myset.erase(itlow,itup); // 10 20 70 80 90
- std::cout << "myset contains:";
- for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
- std::cout << ' ' << *it;
- std::cout << '\n';
- return 0;
- }
- #include "stdafx.h"
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- set<int> myset;
- for (int i=1; i<=5; i++) myset.insert(i*10); // myset: 10 20 30 40 50
- pair<set<int>::const_iterator,set<int>::const_iterator> ret;
- ret = myset.equal_range(30);
- cout << "the lower bound points to: " << *ret.first << '\n';
- cout << "the upper bound points to: " << *ret.second << '\n';
- return 0;
- }
- #include "stdafx.h"
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- int myints[]={12,75,10,32,20,25};
- set<int> first (myints,myints+3); // 10,12,75
- set<int> second (myints+3,myints+6); // 20,25,32
- first.swap(second);
- cout << "first contains:";
- for (set<int>::iterator it=first.begin(); it!=first.end(); ++it)
- cout << ' ' << *it;
- cout << '\n';
- cout << "second contains:";
- for (set<int>::iterator it=second.begin(); it!=second.end(); ++it)
- cout << ' ' << *it;
- cout << '\n';
- return 0;
- }
stl 之set图解的更多相关文章
- 【陪你系列】5 千字长文+ 30 张图解 | 陪你手撕 STL 空间配置器源码
大家好,我是小贺. 点赞再看,养成习惯 文章每周持续更新,可以微信搜索「herongwei」第一时间阅读和催更,本文 GitHub https://github.com/rongweihe/MoreT ...
- 带你深入理解STL之RBTree
最近一直忙于校招的笔试,STL的深入理解系列也耽搁了好几天,再加上!红黑树真的是超级超级难理解,超级超级复杂,参考了好多博客上的大神的理解才稍微明白一点,勉强入个门,下面请以一个菜鸟的角度跟着我一起学 ...
- 带你深入理解STL之Deque容器
在介绍STL的deque的容器之前,我们先来总结一下vector和list的优缺点.vector在内存中是分配一段连续的内存空间进行存储,其迭代器采用原生指针即可,因此其支持随机访问和存储,支持下标操 ...
- 带你深入理解STL之Vector容器
C++内置了数组的类型,在使用数组的时候,必须指定数组的长度,一旦配置了就不能改变了,通常我们的做法是:尽量配置一个大的空间,以免不够用,这样做的缺点是比较浪费空间,预估空间不当会引起很多不便. ST ...
- 详解使用 Tarjan 求 LCA 问题(图解)
LCA问题有多种求法,例如倍增,Tarjan. 本篇博文讲解如何使用Tarjan求LCA. 如果你还不知道什么是LCA,没关系,本文会详细解释. 在本文中,因为我懒为方便理解,使用二叉树进行示范. L ...
- STL标准库-迭代器适配器
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 这次主要介绍一下迭代器适配器.以reverse_iterator(反向迭代器),insert_iterator(插入迭代器),o ...
- STL空间配置器那点事
STL简介 STL(Standard Template Library,标准模板库),从根本上说,STL是一些“容器”的集合,这些“容器”有list,vector,set,map等,STL也是算法和其 ...
- STL源码剖析(heap)
STL heap并不是容器,而是一系列的算法. 这些算法接受RandomAccessIterator[start, end),并将其表述成一棵完全二叉树. 关于heap算法可以参考之前算法导论的一篇博 ...
- 『嗨威说』算法设计与分析 - STL中Sort函数的实现原理初探
本文索引目录: 一.对Sort算法实现的个人阅读体会 二.Sort算法使用的三个排序算法的优点介绍 2.1 插入排序的优缺点 2.2 堆排序的优缺点 2.3 快速排序的优缺点 2.4 新的结合排序—— ...
随机推荐
- myBatis参数处理 myBatis佟刚课程笔记
单个参数:myBatis不会做特殊处理 #{参数名}: 取出参数值 多个参数: myBatis会做特殊处理 多个参数会被封装成一个MAP key:param1 param2.... param10,或 ...
- JavaEE-09 Ajax与jQuery在JavaEE项目中的使用
学习要点 JavaScript实现Ajax jQuery实现Ajax JSON JSON-LIB FastJSON JavaScript实现Ajax 认识Ajax 旧版百度地图 百度搜索自动补全 百度 ...
- HDU6199 gems gems gems (DP)
题意:有n颗石子 两个人轮流拿 如果上一个人拿了x颗 这个人就可以拿x或x+1颗 问先手能获得与后手的价值差最大是多少 题解:看起来是博弈 其实是DP dp[i][j][0/1]表示当前该0/1拿 拿 ...
- vue计算属性computed和methods的区别
computed和methods的区别 在new Vue的配置参数中的computed和methods都可以处理大量的逻辑代码,但是什么时候用哪个属性,要好好区分一下才能做到正确的运用vue. com ...
- 【牛客小白月赛6】F 发电 - 树状数组&快速幂&逆元
题目地址:https://www.nowcoder.com/acm/contest/136/F 树状数组.快速幂.逆元的模板运用: #include<iostream> #include& ...
- 牛客OI赛制测试赛2 C 数组下标
链接:https://www.nowcoder.com/acm/contest/185/C来源:牛客网 题目描述 给出一个数列 A,求出一个数列B. 其中Bi 表示 数列A中 Ai 右边第一个比 ...
- js|jquery常用代码
页面重定位: window.location.replace("http://www.bczs.net"); window.location.href = "http:/ ...
- Python爬虫-Scrapy-CrawlSpider与ItemLoader
一.CrawlSpider 根据官方文档可以了解到, 虽然对于特定的网页来说不一定是最好的选择, 但是 CrwalSpider 是爬取规整的网页时最常用的 spider, 而且有很好的可塑性. 除了继 ...
- LeetCode(71) Simplify Path
题目 Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&quo ...
- PCB线宽与电流计算器--在线计算
http://eda365.com/article-12-1.html 计算线宽与载流量的关系,方便设计:单个人建议在有限的空间尽量将大电流线路加宽.