当前位置: 首页 > news >正文

返利系统网站开发ddns怎么做网站

返利系统网站开发,ddns怎么做网站,中文在线っと好きだっ,旅游网站设计方案怎么做目录 节点 迭代器 整体框架 构造函数 empty_init 拷贝构造 赋值重载 析构函数 clear insert erase push_back和push_front pop_back和push_front size empty Print_Container 节点 对于链表节点#xff0c;我们需要一个数据、一个前驱指针、一个后继指针来维护…目录 节点 迭代器 整体框架 构造函数 empty_init 拷贝构造 赋值重载 析构函数 clear insert erase push_back和push_front pop_back和push_front size empty Print_Container 节点 对于链表节点我们需要一个数据、一个前驱指针、一个后继指针来维护并且将其封装成一个类。 templateclass T struct list_node {T _data;list_nodeT* _next;list_nodeT* _prev;list_node(const T data T()):_data(data),_next(nullptr),_prev(nullptr){} }; 使用struct的原因是因为struct默认的域作用限定符是public方便后续使用不用走友元的那一套。 迭代器 我们知道迭代器提供访问容器的方法之前实现vector和string时迭代器用的就是数据类型的指针但是list不可以直接用。因为vector和string的数据在内存的存放都是连续的如果想找下一个数据的指针迭代器直接迭代器指针就可以了但是list的数据存放在内存不是连续的如果直接把指针当成迭代器迭代器是找不到下一个数据的迭代器。 所以综上所述我们应该用类对链表数据类型的指针封装成迭代器在类里重载操作符让其达到我们想要的效果。 当然我们实现的迭代器应该有两个版本普通版本和const版本。 //普通迭代器 templateclass T struct list_iterator {typedef list_nodeT Node;typedef list_iteratorT Self;Node* _node;list_iterator(Node* node):_node(node){}T operator*(){return _node-_data;}T* 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!(const Self it) const{return _node ! it._node;}bool operator(const Self it) const{return _node it._node;} }; ​//const迭代器 templateclass T struct list_const_iterator {typedef list_nodeT Node;typedef list_const_iteratorT Self;Node* _node;list_const_iterator(Node* node):_node(node){}const T operator*(){return _node-_data;}const T* 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;}bool operator!(const Self it) const{return _node ! it._node;}bool operator(const Self it) const{return _node it._node;} };​ 我们发现这两份代码除了重载*和-有所不同其余代码都是一样的所以我们可以增加两个模板参数将这两份代码合二为一。 templateclass T, class Ref, class Ptr struct list_iterator {typedef list_nodeT Node;typedef list_iteratorT, Ref, Ptr Self;Node* _node;list_iterator(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!(const Self it) const{return _node ! it._node;}bool operator(const Self it) const{return _node it._node;} }; 增加Ref和Ptr模板参数让T*和T作为参数传入这就可以解决将两份代码合二为一。 整体框架 ​ ​ templateclass T class list {typedef list_nodeT Node; public:/*typedef list_iteratorT iterator;typedef list_const_iteratorT const_iterator;*///将T和T*作为参数传入typedef list_iteratorT, T, T* iterator;typedef list_iteratorT, const T, const T* const_iterator;iterator begin(){/*iterator it(_head-_next);return it;*///return iterator(_head-_next);//返回哨兵节点的下一个节点第一个有效节点//隐式类型转换return _head-_next;}iterator end(){//最后一个有效节点的下一位置也就是哨兵节点return _head;}const_iterator begin() const{return _head-_next;}const_iterator end() const{return _head;}//实现各种函数......private:Node* _head;size_t _size; };​​ 构造函数 empty_init 多种构造函数的代码都有重合所以把重合部分独立成一个函数。 ​ void empty_init() {//创造哨兵节点_head new Node();_head-_next _head;_head-_prev _head;_size 0; }​ 普通构造 普通构造就是创造哨兵节点调用empty_init即可。 //普通构造 list() {empty_init(); } 列表构造 C11的用法用法例子如下 listint lt1 { 1,2,3,4,5,6 }; 先创造一个哨兵节点然后将列表的元素尾插即可。 //列表构造 list(initializer_listT il) {empty_init();for (auto e : il){push_back(e);} } 关于列表initializer_listT的知识可以看以下连接。 介绍列表 拷贝构造 创建哨兵节点将链表元素尾插到待构造的链表就完成拷贝构造了。 //拷贝构造 list(const listT lt) {empty_init();for (auto e : lt){push_back(e);} } 赋值重载 与临时对象lt交换即可跟string、vector的实现类似。 void swap(listT lt) {std::swap(_head, lt._head);std::swap(_size, lt._size); }listT operator(listT lt) {swap(lt);return *this; } 析构函数 clear 清理除了哨兵节点以外的所有节点。 void clear() {auto it begin();while (it ! end()){it erase(it);} } 先将链表clear掉然后清理哨兵节点。   ~list() {clear();delete _head;_head nullptr; } insert 在pos迭代器位置前插入元素x插入后_size返回新插入元素的迭代器。 iterator insert(iterator pos, const T x) {Node* cur pos._node; //pos是iterator类的对象访问里面的成员变量用pos._node不能用pos-_nodeNode* prev cur-_prev;Node* newnode new Node(x);newnode-_next cur;cur-_prev newnode;newnode-_prev prev;prev-_next newnode;_size;//隐式类型转换return newnode; } erase 删除pos位置的元素删除后_size--返回删除元素的下一元素的迭代器。 iterator erase(iterator pos) {assert(pos ! end()); //不能删掉哨兵位的节点Node* prev pos._node-_prev;Node* next pos._node-_next;prev-_next next;next-_prev prev;delete pos._node; --_size;return next; } push_back和push_front 利用insert函数就可以实现尾插和头插。 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;_size;*/ insert(end(), x); }void push_front(const T x) {insert(begin(), x); } pop_back和push_front 利用erase函数实现尾删和头删。 void pop_back() {erase(--end()); }void pop_front() {erase(begin()); } size 返回链表有效元素的个数.。 size_t size() const {return _size; } empty 判断链表是否为空。 bool empty() const {return _size 0; } Print_Container 打印容器的函数。 templateclass Container void Print_Container(const Container con) {//const对象要用const迭代器这里没实现的话会报错/*auto it con.begin();while (it ! con.end()){cout *it ;it;}*/for (auto e : con){cout e ;}cout endl; } 拜拜下期再见 摸鱼ing✨
http://www.w-s-a.com/news/977191/

相关文章:

  • 宜昌建设厅网站中国最新时事新闻
  • 微网站怎么开发wordpress 发表评论
  • 山东网站建设是什么一页网站首页图如何做
  • 游戏开发与网站开发哪个难万网影
  • 做网站编程语言建筑施工特种证书查询
  • 找人做网站内容自己编辑吗修改wordpress登陆界面
  • 登陆建设银行wap网站湖南网站建设磐石网络答疑
  • 58网站怎么做浏览度才高论坛网站怎么做排名
  • wordpress 手机网站支付京东网站建设的经费预算
  • 自己怎么样做游戏网站做海外贸易网站
  • 建立什么样的网站好制作网页网站代码
  • 岳麓区专业的建设网站公司尚一网常德论坛
  • 电商网站建设实训报告360站长平台链接提交
  • 个性化网站建设公司个人网站备案类型
  • 腾讯建站模板上海网站开发有限公司
  • 网站和小程序的区别请问做网站怎么赚钱
  • 网站logo设计免费版在线网站开发建设准备工作
  • wordpress多站点 主题南京做网站好的公司
  • 广州 门户seo到底是做什么的
  • 可以登录国外网站吗如何用家用电脑做网站
  • 吉安建站公司wordpress企业
  • 河北住房和城乡建设厅网站6thinkphp做视频网站
  • 遵义网站制作一般需要多少钱深圳全国网站制作哪个好
  • 公众平台网站价格哪个网站做餐饮推广最好
  • 深圳 公司网站设计重庆的网站设计公司价格
  • 网站开发市场分析餐饮平台app有哪些
  • 制作一个收费网站要多少钱开发网站需要什么技术
  • 网站流量统计平台二手域名做网站不收录
  • 蒙古网站后缀mysql8.0 wordpress
  • 免费建立一个网站互联网推广培训