list 简化版模拟实现

 1ListNode

template<class T>struct ListNode{public:ListNode(const T& x = T()):_next(nullptr), _prev(nullptr), _data(x){}//private://共有可访问ListNode<T>* _next;ListNode<T>* _prev;T _data;};

 实现iterator对Node*的封装  实现运算符重载

 

void const_print(const MyList::list<int>& lt)//这里的const本意是lt的node中的data不可改,却使lt不可改
//const迭代器是指, it已经封装过,把it当作指向node中的data的指针    it内的_node 的
{MyList::list<int>::const_iterator it = lt.begin();//所以应该是 通过函数重载,返回一个迭代器,不能支持对(*it)的改动while (it != lt.end()){//报错,必须是可修改的左值  *it += 19;//*it += 19;cout << (*it) << ' ';it++;}cout << endl;
}

 2ListIterator

template<class T, class Ref, class Ptr>class ListIterator{public:typedef ListNode<T> Node;typedef ListIterator<T, Ref, Ptr> Self;//变化ListIterator(Node* node):_node(node){}Ref operator*()//变化{return _node->_data;}Ptr operator->()//变化{return &(_node->_data);}Self& operator++(){_node = _node->_next;return *this;}Self operator++(int){Self tmp = *this;_node = _node->_next;return tmp;}Self& operator--(){_node = _node->_prev;return *this;}Self operator--(int){Self tmp = *this;_node = _node->_prev;return tmp;}bool operator!=(Self it){return _node != it._node;}//private:   共有Node* _node;};

 3list

template<class T>class list{public:typedef ListNode<T> Node;//typedef ListIterator<T> iterator;//typedef ListConstIterator<T> const_iterator;//但这样要实现两个类 可以合并typedef ListIterator<T, T&, T*> iterator;typedef ListIterator<T, const T&, const  T*> const_iterator;iterator begin()//头是head下一个节点{return _head->_next;//单参数的自定义类型支持隐式类型转化  c++11对于多参数的也可以{xx,xx,xx}}iterator end()//尾是head{return _head;}const_iterator begin()const//直接加const 对于const对象能调用,然后刚好构成函数重载,{return _head->_next;//返回值也不能是 const itertor 这指的是it不能改,应该封装成(*it)不可改}const_iterator end()const{return _head;}void empty_init(){//先new一个节点_head = new Node;_head->_next = _head;_head->_prev = _head;}list(initializer_list<T>il){empty_init();for (auto& e : il)push_back(e);}list(){empty_init();}list(const list<T>& lt){empty_init();for (auto& i : lt)//要引用 有可能是自定义类型{push_back(i);}}/*void push_back(const T& x){Node* newnode = new Node(x);Node* tail = _head->_prev;tail->_next = newnode;newnode->_prev = tail;newnode->_next = _head;_head->_prev = newnode;}*/void push_back(const T& x){insert(end(), x);}void push_front(const T& x){insert(begin(), x);}void pop_back(){erase(--end());//不能是-1,没有重载}void pop_front(){erase(begin());}void insert(iterator pos, T val){Node* cur = pos._node;Node* prev = cur->_prev;Node* newnode = new Node(val);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;}iterator erase(iterator pos)//迭代器失效  返回next{Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;prev->_next = next;next->_prev = prev;delete cur;//return iterator(next);}size_t size(){size_t sz = 0;for (auto i : (*this))sz++;return sz;}bool empty(){return size() == 0;}void clear(){iterator it = begin();while (it != end()){it = erase(it);}}~list(){clear();delete _head;}private:Node* _head;};

 4对于const迭代器的解释和->操作符重载

struct A
{int _a1;int _a2;A(int a1 = 0, int a2 = 0):_a1(a1), _a2(a2){}
};
void const_print(const MyList::list<int>& lt)//这里的const本意是lt的node中的data不可改,却使lt不可改(
//const迭代器是指, it已经封装过,把it当作指向node中的data的指针    
{MyList::list<int>::const_iterator it = lt.begin();//所以应该是 通过函数重载,返回一个迭代器,不能支持对(*it)的改动while (it != lt.end()){//报错,必须是可修改的左值  *it += 19;//*it += 19;cout << (*it) << ' ';it++;}cout << endl;
}
void print(MyList::list<int>& lt)
{MyList::list<int>::iterator it = lt.begin();while (it != lt.end()){*it += 19;cout << (*it) << ' ';it++;}cout << endl;
}
void test2()
{MyList::list<A>lt;lt.push_back({ 1,2 });//隐式类型转换,c++11lt.push_back({ 1,2 });lt.push_back({ 1,2 });MyList::list<A>::iterator it = lt.begin();while (it != lt.end()){//报错cout << (*it) << " ";//(*it)访问的是 节点的数据 对于int 数据就是int 支持流输出 ,这里是A对象//其实就是把it当作 指向A的指针,但本质不是    (*it)就是A  //就是(*it)经过封装,(*it)就是A  ,,但it->没封装//cout << (*it)._a1 << "." << (*it)._a2<<endl;//但本应该it->data  要支持->重载  返回值是_data *cout << it->_a1 << "." << it->_a2 << endl;it++;}cout << endl;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/618796.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

二分查找-java

二分查找-java 二分查找基础版改动版平衡版 二分查找高级用法LeftMost0102 RightMost0102 LeftMost和RightMost的方式二的高级用法 二分查找 基础版 arr是待查找升序数组&#xff0c;target是待查找元素 基础版的left和right两个所指向的元素都是待查找值 public static int b…

【1000个GDB技巧之】如何在远端服务器打开通过vscode动态观测Linux内核实战篇?

Step: 配置ssh的服务端host &#xff08;也可以直接在vscode中配置&#xff0c;忽略&#xff09; 主要步骤&#xff1a;在~/.ssh/config中添加服务端的host&#xff0c;以便vscode的remote中能够登录 详细配置过程参考兄弟篇文章&#xff1a;ssh config如何配置用host名替代ro…

【Linux】阿里云ECS搭建lnmp和lamp集群

搭建LNMP&#xff08;Linux Nginx MySQL PHP&#xff09;或LAMP&#xff08;Linux Apache MySQL PHP&#xff09;集群 创建ECS实例&#xff1a; 在阿里云控制台创建多个ECS实例&#xff0c;选择相应的操作系统和配置&#xff0c;确保这些实例在同一VPC网络内&#xff0c;…

系统架构最佳实践 -- 金融企业的资损问题介绍

什么是资损 资损通常来讲是指支付场景下的资金损失&#xff0c;这里可以从两个维度看 用户角度&#xff1a;多扣用户款导致用户资金损失&#xff0c;此问题一般需要通过客服等渠道反馈&#xff0c;可以把多的钱退给用户&#xff0c;但是很大程度上损失了用户体验&#xff1b; …

ssh爆破服务器的ip-疑似肉鸡

最近发现自己的ssh一直有一些人企图使用ssh暴力破解的方式进行密码破解.就查看了一下,真是网络安全太可怕了. 大家自己的服务器密码还是要设置好,管好,做好最基本的安全措施,不然最后只能沦为肉鸡. ssh登陆日志可以在/var/log下看到,ubuntu的话为auth.log,centos为secure文件 查…

【算法】回溯:与递归,dfs的同质与分别,剪枝与恢复现场的详细理解,n皇后的回溯解法及算法复杂度分析。

目录 ​编辑 1.什么是回溯 2.关于剪枝 3.关于恢复现场 4.题目&#xff1a;二叉树的所有路径&#xff08;凸显恢复现场&#xff1a;切实感受回溯与深搜&#xff09; 问题分析 ①函数设置为&#xff1a;void Dfs(root) ②函数设置为&#xff1a;void Dfs(root,path) 解题思想&…

微信小程序 发送消息 Token校验失败,请检查确认

如上图&#xff0c;文档中说的是 开发者通过检验 signature 对请求进行校验&#xff08;下面有校验方式&#xff09;。若确认此次 GET 请求来自微信服务器&#xff0c;请原样返回 echostr 参数内容&#xff0c;则接入生效&#xff0c;成为开发者成功&#xff0c;否则接入失败。…

深度学习图像处理基础工具——opencv 实战信用卡数字识别

任务 信用卡数字识别 穿插之前学的知识点 形态学操作 模板匹配 等 总体流程与方法 1.有一个模板 2 用轮廓检测把模板中数字拿出来 外接矩形&#xff08;模板和输入图像的大小要一致 &#xff09;3 一系列预处理操作 问题的解决思路 1.分析准备&#xff1a;准备模板&#…

[管理者与领导者-163] :团队管理 - 高效执行力 -1- 高效沟通的架构、关键问题、注意事项

目录 前言&#xff1a;沟通是管理者实施管理最重要的工作 一、人与人沟通模型 1.1 模型 1.2 完整过程 1.3 发送和接受方式 1.4 传输 1.5 关于编码与解码 1.6 反馈 1.7 沟通中常见问题 二、管理者如何提高沟通的效率 2.1 为什么管理者布置任务后&#xff0c;总有人…

数据仓库—维度建模—维度表设计

维度表 维度表(Dimension Table)是数据仓库中描述业务过程中各种维度信息的表,用于提供上下文和描述性信息,以丰富事实数据的分析 维度表是维度建模的灵魂所在,在维度表设计中碰到的问题(比如维度变化、维度层次、维度一致性、维度整合和拆分等)都会直接关系到维度建模…

pyqt和opencv结合01:读取图像、显示

在这里插入图片描述 1 、opencv读取图像用于pyqt显示 # image cv2.imread(file_path)image cv2.cvtColor(image, cv2.COLOR_BGR2RGB)# 将图像转换为 Qt 可接受的格式height, width, channel image.shapebytes_per_line 3 * widthq_image QImage(image.data, width, hei…

OLTP 与 OLAP 系统说明对比和大数据经典架构 Lambda 和 Kappa 说明对比——解读大数据架构(五)

文章目录 前言OLTP 和 OLAPSMP 和 MPPlambda 架构Kappa 架构 前言 本文我们将研究不同类型的大数据架构设计&#xff0c;将讨论 OLTP 和 OLAP 的系统设计&#xff0c;以及有效处理数据的策略包括 SMP 和 MPP 等概念。然后我们将了解经典的 Lambda 架构和 Kappa 架构。 OLTP …