当前位置:网站首页>map和set的实现
map和set的实现
2022-07-06 21:03:00 【芜湖开冲~】
前面我们实现了红黑树,https://blog.csdn.net/qq_55143256/article/details/125118713
今天我们来用用map和set对红黑树进行封装
目录
map
KeyOfValue
如果你仔细看一下红黑树的实现代码,就会发现有一个东西很奇怪,就是KeyOfValue,那这是干什么的呢?就是封装map的时候需要使用的
它在map里面的作用就是对map里面的键值对,提取其中的key,因为我们map比较的时候,其实是比较的键值对里面的key的
[]重载
map里面的[]符号不仅可以取key对应的value,还可以对插入,就是如果没有这个key,就会将这个key插进去
这是官方给的注释,我们就按照它给的注释写就可以了
其他就没有什么注意的,因为map就是对原本的红黑树进行的封装
代码
#pragma once
#include "RBTree.hpp" //这里的这个头文件就是红黑树博客里面的所有代码组成的头文件
namespace cdd {
template<class K, class V>
class map {
typedef pair<K, V> ValueType;
struct KeyOfValue {
const K& operator()(const ValueType& value) {
return value.first;
}
};
typedef RBTree<ValueType, KeyOfValue> tree;
public:
typedef typename tree::iterator iterator;
public:
map()
:_t()
{}
iterator begin()
{
return _t.Begin();
}
iterator end()
{
return _t.End();
}
size_t size()const
{
return _t.Size();
}
bool empty()const
{
return _t.Empty();
}
V& operator[](const K& key)
{
return (*(_t.Insert(make_pair(key, V())).first)).second;
}
pair<iterator, bool> insert(const ValueType& value)
{
return _t.Insert(value);
}
void swap(map<K, V>& m)
{
_t.Swap(m._t);
}
void clear()
{
_t.Clear();
}
iterator find(const K& key)
{
return _t.Find(make_pair(key, V()));
}
private:
tree _t;
};
}
标准库map相关知识点
标准库为 map 容器配备的是双向迭代器(bidirectional iterator)。这意味着,map 容器迭代器只能进行 ++p、p++、–p、p–、*p 操作,并且迭代器之间只能使用 == 或者 != 运算符进行比较。
值得一提的是,相比序列式容器,map 容器提供了更多的成员方法(如表 1 所示),通过调用它们,我们可以轻松获取具有指定含义的迭代器。
map容器默认排序规则为:按照key值进行从小到大的排序,当然,我么可以利用仿函数进行排序
set
set就更没有什么难的了,它没有[]重载,也不是键值对,所以实在没有什么有问题的地方
代码
#pragma once
#include "RBTree.hpp"
namespace zds {
template<class K>
class set {
struct KeyOfValue {
const K& operator()(const K& value) {
return value;
}
};
typedef RBTree<K, KeyOfValue> tree;
public:
typedef typename tree::iterator iterator;
public:
set()
:_t()
{}
iterator begin()
{
return _t.Begin();
}
iterator end()
{
return _t.End();
}
size_t size()const
{
return _t.Size();
}
bool empty()const
{
return _t.Empty();
}
pair<iterator, bool> insert(const K& value)
{
return _t.Insert(value);
}
void swap(set<K>& s)
{
_t.Swap(s._t);
}
void clear()
{
_t.Clear();
}
iterator find(const K& key)
{
return _t.Find(key);
}
private:
tree _t;
};
}
标准库set的相关知识点
set里面的方法
begin() ,返回set容器的第一个元素
end() ,返回set容器的最后一个元素
clear() ,删除set容器中的所有的元素
empty() ,判断set容器是否为空
max_size() ,返回set容器可能包含的元素最大个数
size() ,返回当前set容器中的元素个数
rbegin ,返回的值和end()相同
rend() ,返回的值和rbegin()相同
count() ,用来查找set中某个某个键值出现的次数。这个函数在set并不是很实用,因为一个键值在set只可能出现0或1次,这样就变成了判断某一键值是否在set出现过了
erase(iterator) ,删除定位器iterator指向的值
erase(first,second),删除定位器first和second之间的值
erase(key_value),删除键值key_value的值
find() ,返回给定值值得定位器,如果没找到则返回end()。
这些是一些比较常用的,其实set最大的功能是对一组元素排序去重,嗯,就是说它本身的性质就是最大的功能
最后,有什么问题欢迎评论区留言
边栏推荐
- Not All Points Are Equal Learning Highly Efficient Point-based Detectors for 3D LiDAR Point
- Free PHP online decryption tool source code v1.2
- About Confidence Intervals
- Tencent cloud native database tdsql-c was selected into the cloud native product catalog of the Academy of communications and communications
- VHDL实现任意大小矩阵加法运算
- QT opens a file and uses QFileDialog to obtain the file name, content, etc
- The true face of function pointer in single chip microcomputer and the operation of callback function
- About Tolerance Intervals
- When QT uses qtooltip mouse to display text, the picture of the button will also be displayed and the prompt text style will be modified
- Search of linear table
猜你喜欢
2022.6.28
Open3D 网格滤波
概率论公式
It's too convenient. You can complete the code release and approval by nailing it!
24. (ArcGIS API for JS) ArcGIS API for JS point modification point editing (sketchviewmodel)
太方便了,钉钉上就可完成代码发布审批啦!
20.(arcgis api for js篇)arcgis api for js面采集(SketchViewModel)
1200.Minimum Absolute Difference
2022夏每日一题(一)
ubuntu20安裝redisjson記錄
随机推荐
pip只下载不安装
链表面试常见题
哈夫曼树基本概念
接口数据安全保证的10种方式
枚举通用接口&枚举使用规范
本机mysql
ubuntu20安装redisjson记录
机器学习笔记 - 使用机器学习进行鸟类物种分类
再AD 的 界面顶部(菜单栏)创建常用的快捷图标
卡尔曼滤波-1
About Tolerance Intervals
Search of linear table
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
Under the tide of "going from virtual to real", Baidu AI Cloud is born from real
Basic concepts of Huffman tree
浅谈网络安全之文件上传
力扣------路径总和 III
Adaptive non European advertising retrieval system amcad
Flutter3.0, the applet is not only run across mobile applications
Vernacular high concurrency (2)