2.3 set
来源:互联网 发布:阿里云cdn测评 编辑:程序博客网 时间:2024/06/02 04:13
#include<set>
红黑树(Red-Black Tree),一种平衡二叉检索树。
对于插入相同键值的情况忽略处理。
set主要用于快速检索
高效的插入和删除
multiset、map、multimap都是平衡二叉检索树。
创建set集合:
set<int> s;
元素的插入与中序遍历:
s.insert(3);for(set<int>::iterator it = s.begin(); it != s.end(); ++it) cout << *it << " ";
元素的反向遍历:
for(set<int>::reverse_iterator rit = s.rbegin(); rit != s.rend(); ++rit) cout << *rit << " ";
元素的删除:
可以删除某个迭代器位置上的元素、等于某键值的元素、一个区间上的元素
s.erase(4); //删除键值为4的元素
清空集合
s.clear();
元素的检索:
set<int>::iterator it = s.find(3); //如果找到,返回迭代器位置;如果未找到,返回end()。
自定义比较函数:
两种方式:
1、元素是结构体,则直接重载操作符'<';
2、元素是基本元素,则构建一个结构体,里面重载'()'操作符,逻辑代码实现比较功能。
struct desComp{ bool operator()(const int &a, const int &b) { if (a != b) return a > b; else return a > b; }}set<int, desComp>s;s.insert(3);... ...for (set<int, desComp>::iterator it = s.begin(); it != s.end(); ++it) cout << *it << " ";
1 0
- 2.3 set
- set /?
- Set
- set
- set
- set
- Set
- set
- Set
- Set
- set
- set
- Set
- set
- Set
- set
- Set
- set
- 洛谷 P1255 数楼梯
- lucene学习笔记(一)简介
- C语言希尔排序的实现
- 记住密码、自动登录、显示密码
- 自适应屏幕的滚动栏
- 2.3 set
- Soldier and Number Game
- 洛谷 P1303 A*B Problem
- 嵌入式Linux学习:u-boot源码分析(3)--AM335X系列的2014.10版
- 从0到1学习node之简易的网络爬虫
- Detection Green Balls
- 洛谷 P1604 B进制星球
- C语言冒泡排序的实现
- unit 11-12作业