STL-RBT_map,set模拟实现
set
#include"26RBT_container.h"
namespace test
{
//set通过普通迭代器使用const迭代器实现限制key不能被修改 template<class K>
class set
{ private:
struct setKeyOfT//命名? 返回RBT的类型T接收的 set的key
{
const K& operator()(const K& key) //类似函数重载
{
return key; //返回key --重载原函数
}
};
private:
RBTree< K, K, setKeyOfT>_t;
public:
//使用类域的要加typename,以防和静态变量冲突
typedef typename RBTree<K, K, setKeyOfT>::const_iterator iterator; //普通迭代器
typedef typename RBTree<K, K, setKeyOfT>::const_iterator const_iterator; //const迭代器
/**
* set中因为key不能修改,所以RBT的模板参数T位置需要是const K,但是,const迭代器中的是const T,使用const K的话会const迭代器变成const const K
* 解决:set的普通迭代器也使用const迭代器
*
*/ iterator begin()
{
return _t.begin(); //begin是普通迭代器,返回值是const,发生隐式类型转换(单参数)
//如果有相应的构造函数,则支持隐式类型转换 ,但此时迭代器没有参数为迭代器的构造函数,需要添加
//
}
iterator end()
{
return _t.end();
} const_iterator begin()const
{
return _t.begin();
}
const_iterator end()const
{
return _t.end();
} public:
pair<iterator, bool> insert(const K& key)
{
return _t.insert(key);
} }; void test_mySet1()
{
test::set<int> s;
s.insert(2);
s.insert(1);
s.insert(3); set<int>::iterator it = s.begin();
//while (it!=s.end())
//{
// cout << *it << " ";
// ++it;
//}
for (auto& e : s)
{
cout << e << " ";
}
cout << *++it << " ";
cout << *--it << " ";
cout << endl;
//*it = 1;////不允许赋值,表达式必须是可以修改的左值 .不能给常量赋值 }
}
map:
#pragma once #include"26RBT_container.h"
namespace test
{
template<class K,class V>
class map
{ //map通过pair中的const K 限制了Key不能被修改 private:
struct mapKeyOfT//命名? 返回RBT的类型T接收的 map的key
{
const K& operator()(const pair<const K,V>& kv) //类似函数重载
{
return kv.first; //返回pair的key
}
};
private:
RBTree<K, pair<const K, V>, mapKeyOfT> _t;
public:
typedef typename RBTree<K, pair<const K,V>, mapKeyOfT>::iterator iterator; //使用类域的要加typename,以防和静态变量冲突 iterator begin()
{
return _t.begin();
}
iterator end()
{
return _t.end();
} public:
pair<iterator,bool> insert(const pair<const K, V>& kv)
{
return _t.insert(kv);
} V& operator[](const K& key)
{
pair<iterator,bool> ret = insert(make_pair(key, V()));
return ret.first->second;
} }; void test_myMap1()
{
test::map<int, int> m;
m.insert(std::make_pair(1, 1));
m.insert(std::make_pair(3, 3));
m.insert(std::make_pair(2, 2));
test::map<int,int>::iterator it = m.begin();
//while (it != m.end())
//{
// cout << it->first << " ";
// ++it;
//}
for (auto e : m)
{
cout << e.first << " ";
} cout << (++it)->first << " ";
cout << (--it)->first << " ";
cout << endl; //it->second = 1; //可以赋值
//it->first = 1;//不允许赋值,表达式必须是可以修改的左值 .不能给常量赋值
} void test_myMap2()
{
//map的使用
map<std::string, std::string> dict;
dict.insert(std::pair<std::string, std::string>("sort", "排序")); //匿名对象插入
dict.insert(std::make_pair("string", "字符串")); //pair封装插入
dict.insert(std::make_pair("count", "计数"));
dict.insert(std::make_pair("count", "(计数)")); //插入失败的
auto it = dict.begin();
while (it != dict.end())
{
cout << it->first << ":" << it->second << endl;
++it;
}
} void test_myMap3()
{
std::string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉" };
map<std::string, int> countMap;
/*for (auto e : arr)
{
auto ret = countMap.find(x);
if (ret==countMap.end())
{
countMap.insert(std::pair<string, int>(x, 1));
}
else
{
++ret->second;
}
}*/ for (auto& e : arr)
{
++countMap[e];
} for (auto& s : countMap)
{
cout << s.first << ":" << s.second << endl;
} } }
STL-RBT_map,set模拟实现的更多相关文章
- 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn
题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边 ...
- 利用C++ STL的vector模拟邻接表的代码
关于vector的介绍请看 https://www.cnblogs.com/zsq1993/p/5929806.html https://zh.cppreference.com/w/cpp/conta ...
- 一些有用的stl知识《acm程序设计》
accepted 通过 Presentation Error 输出格式错误 Wrong Answer 答案错误 Runtime Error 多为数组访问越界 程序运行时 ...
- [C++11][数据结构]自己的双链表实现
这个双链表,是我模仿stl的list制作的,只实现了一些基本功能,像merge,transfer这些就没有实现,用户可以用基本操作来自己做外部实现. 我没有选用stl的[begin,end)迭代器模式 ...
- [SinGuLaRiTy] NOIP2017 提高组
[SinGuLaRiTy-1048] Copyright (c) SinGuLaRiTy 2018. All Rights Reserved. NOIP2017过了这么久,现在2018了才找到寒假这么 ...
- PAT_A1153#Decode Registration Card of PAT
Source: PAT A1153 Decode Registration Card of PAT (25 分) Description: A registration card number of ...
- PAT_A1141#PAT Ranking of Institutions
Source: PAT A1141 PAT Ranking of Institutions (25 分) Description: After each PAT, the PAT Center wil ...
- CCF-CSP认证 C++题解目录
持续更新中,记录刷题过程并分享一下小小的心得总结. 试题编号 试题名称 标签 202006-1 线性分类器 | 题解 线性规划 202006-2 稀疏向量| 题解 归并排序思想 202006-3 化学 ...
- stl+模拟 CCF2016 4 路径解析
// stl+模拟 CCF2016 4 路径解析 // 一开始题意理解错了.... #include <iostream> #include <string> #include ...
- CodeForces 705C Thor (模拟+STL)
题意:给定三个操作,1,是x应用产生一个通知,2,是把所有x的通知读完,3,是把前x个通知读完,问你每次操作后未读的通知. 析:这个题数据有点大,但可以用STL中的队列和set来模拟这个过程用q来标记 ...
随机推荐
- webservice--WSDL文件生成本地的代理类
我们在对应第三方接口时常用:项目上右键---->服务引用---->WCF Web Service,如下图的页面----->填好url后---->转到,就可以发现服务,生成代理类 ...
- TienChin 渠道管理-查看渠道接口
自定义 hasPermission 校验规则 自定义一个 Spring Security hasPermission 校验规则: 在 tienchin-framework 模块当中进行自定义,新建 C ...
- go中的sync.RWMutex源码解读
读写锁 前言 什么是读写锁 看下实现 读锁 RLock RUnlock 写锁 Lock Unlock 问题要论 写操作是如何阻止写操作的 写操作是如何阻止读操作的 读操作是如何阻止写操作的 为什么写锁 ...
- SqlSugar基础查询
查所有 List<Student> list=db.Queryable<Student>().ToList() //select * from Student 查询总数 int ...
- 三线表制作(word)
三线表制作 转载:https://blog.csdn.net/zaishuiyifangxym/article/details/81668886
- 【3】超级详细matplotlib使用教程,手把手教你画图!(多个图、刻度、标签、图例等)
相关文章: 全网最详细超长python学习笔记.14章节知识点很全面十分详细,快速入门,只用看这一篇你就学会了! [1]windows系统如何安装后缀是whl的python库 [2]超级详细Pytho ...
- Postfix + Extmail 企业邮件服务器搭建
ExtMail套件用于提供从浏览器中登录.使用邮件系统的Web操作界面,而Extman套件用于提供从浏览器中管理邮件系统的Web操作界面.它以GPL版权释出,设计初衷是希望设计一个适应当前高速发展的I ...
- Hadoop超详细讲解之单节点搭建
1 Hadoop介绍 Hadoop是Apache旗下的一个用java语言实现开源软件框架,是一个开发和运行处理大规模数据的软件平台.允许使用简单的编程模型在大量计算机集群上对大型数据集进行分布式处理. ...
- iOS测试包的安装方法
iOS测试包根据要安装的机器类型可以分为2种: .app模拟器测试包 .ipa真机测试包 .app模拟器测试包的安装方式 方式一:Xcode生成安装包 1.Xcode运行项目,生成app包 2.将AP ...
- AOKO奥科美2.5英寸外置硬盘盒开箱
上次在坛子里发布了一个帖子,然后根据坛友们的反馈,换购了另一个SATA固态硬盘.另一个是配套的硬盘盒,当时在某宝上搜了一圈,最终购买了这款硬盘盒,主要是因为它的外观,旁边有散热片.这款硬盘盒在某宝上不 ...