当前位置:网站首页>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最大的功能是对一组元素排序去重,嗯,就是说它本身的性质就是最大的功能
最后,有什么问题欢迎评论区留言
边栏推荐
- About Estimation Statistics
- 【开发软件】 tilipa开发者软件
- Enumeration general interface & enumeration usage specification
- 自适应非欧表征广告检索系统AMCAD
- Introduction to opensea platform developed by NFT trading platform (I)
- Top 50 hit industry in the first half of 2022
- How to customize the shortcut key for latex to stop running
- 24.(arcgis api for js篇)arcgis api for js点修改点编辑(SketchViewModel)
- 21. (article ArcGIS API for JS) ArcGIS API for JS rectangular acquisition (sketchviewmodel)
- RestClould ETL 社区版六月精选问答
猜你喜欢

复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算

23. (ArcGIS API for JS) ArcGIS API for JS ellipse collection (sketchviewmodel)

21. (article ArcGIS API for JS) ArcGIS API for JS rectangular acquisition (sketchviewmodel)

20.(arcgis api for js篇)arcgis api for js面采集(SketchViewModel)

Under the tide of "going from virtual to real", Baidu AI Cloud is born from real

未来发展路线确认!数字经济、数字化转型、数据...这次会议很重要

太方便了,钉钉上就可完成代码发布审批啦!

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

Preprocessing - interpolation

QT 打开文件 使用 QFileDialog 获取文件名称、内容等
随机推荐
. Net interface can be implemented by default
Sorting operation partition, argpartition, sort, argsort in numpy
Cryptography series: detailed explanation of online certificate status protocol OCSP
web服务性能监控方案
HW-小记(二)
First understand the principle of network
接口数据安全保证的10种方式
浅谈网络安全之文件上传
C# Task拓展方法
华为小米互“抄作业”
Machine learning notes - bird species classification using machine learning
Can the applet run in its own app and realize live broadcast and connection?
[development software] tilipa Developer Software
QT opens a file and uses QFileDialog to obtain the file name, content, etc
海思3559万能平台搭建:RTSP实时播放的支持
My brave way to line -- elaborate on what happens when the browser enters the URL
About Estimation Statistics
21. (article ArcGIS API for JS) ArcGIS API for JS rectangular acquisition (sketchviewmodel)
Class常量池与运行时常量池
Open3d mesh filtering