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

自媒体一号WordPress主题兰州网站关键字优化

自媒体一号WordPress主题,兰州网站关键字优化,百度推广是怎么做的,注册网站要多久前面学习了vector和string,接下来剖析stl中的list,在数据库中学习过,list逻辑上是连续的,但是存储中是分散的,这是与vector这种数组类型不同的地方。所以list中的元素设置为一个结构体,将list设计成双向的&…

前面学习了vector和string,接下来剖析stl中的list,在数据库中学习过,list逻辑上是连续的,但是存储中是分散的,这是与vector这种数组类型不同的地方。所以list中的元素设置为一个结构体,将list设计成双向的:

	template <class T>struct list_node{list_node<T>* _next;list_node<T>* _pre;T _data;list_node(const T& val=T()):_next(nullptr),_pre(nullptr),_data(val){}};

接下来是stl中的迭代器部分,list的迭代器还可以像vector这种原生指针是的使用方式使用吗?答案是否定的,因为vector的底层存储是连续的,并且迭代器就是vector内部元素指针的简单封装。所以底层自然实现起来比较简单,但是list的底层不可以,比如迭代器++,vector本来就是连续的,所以直接就能用,但是struct不是连续的,所以要自己实现,否则谁知道会++到哪里造成访问未知空间。所以既然它不支持我们直接使用,那我们就造一个迭代器出来,这个迭代器的核心就是node:

	template <class T, class PTR, class PEF>struct _list_iterator{typedef list_node<T> Node;typedef _list_iterator<T,PTR,PEF> self;Node* _node;//核心_list_iterator(Node* node):_node(node){}PEF operator*(){return _node->_data;}//要可以返回值、还能修改!PTR operator->(){return &(operator*());}self& operator++(){_node = _node->_next;return *this;}//后置++self operator++(int){self tmp(*this);_node = _node->_next;return tmp;}self& operator--(){_node = _node->_pre;return *this;}//后置--self operator--(int){self tmp(*this);_node = _node->_pre;return tmp;}bool operator!=(const self& it){return _node != it._node;}bool operator==(const self& it){return _node == it._node;}};

这里其他的地方没有太大的疑问,就是operator->发现了,返回的是nodedata的地址,首先咱们来看,当T是内置类型int时,是没有必要用这个的吧?直接*就可以得到,但是如果T时自定义类型的呢?比如:

	struct A{int _a;int _b;A(int a = 1,int b=1):_a(a),_b(b){}};void test2(){list<A> l;l.push_back(A(1,1));l.push_back(A(2, 2));l.push_back(A(3, 3));l.push_back(A(4, 4));std::cout << it->_a << " " << it->_b << " ";list<A>::iterator it = l.begin();}

此时我想访问A中的数据,我当然可以重载<<这个运算符了,但是在stl中我们不知道自定义类型中会有什么数据,所以stl中是不会预先,也不能写出来<<运算符。所以就有了->符,我们可以自己访问struct中的数据,但是还有一个问题,->返回struct的地址,还应该再来一个->吧,就是it->->_a,这里编译器做了优化,所以只需要一个->就可以。

看list:

	template <class T>class list{typedef list_node<T> Node;public:typedef _list_iterator<T,T*,T&> iterator;typedef _list_iterator<T,const T*,const T&> const_iterator;iterator begin(){return iterator(_head->_next);}iterator end(){return iterator(_head);}const_iterator begin() const{return const_iterator(_head->_next);}const_iterator end() const{return const_iterator(_head);}list(){_head = new Node;_head->_next = _head;_head->_pre = _head;}//void push_back(Node newnode)//{//	Node* tail = _head->_pre;//	tail->_next = newnode;//	newnode->_pre = tail;//	newnode->_next = _head;//	_head->_pre = newnode;//}void push_back(const T& val){Node* newnode = new Node(val);Node* tail = _head->_pre;tail->_next = newnode;newnode->_pre = tail;newnode->_next = _head;_head->_pre = newnode;}private:Node* _head;};

list的形式:带哨兵位的双向列表

解释模板在const迭代器和普通迭代器中的巧妙使用,通过模板避免多写代码:

 巧妙使用模板参数。

list迭代器失效的场景只存在于删除结点时,他不像vector这种连续的,插入了会导致其中大部分元素发生移动,所以只会在删除结点时,如何解决?返回删除结点的下一个节点的迭代器。

附全部代码:

	template <class T>struct list_node{list_node<T>* _next;list_node<T>* _pre;T _data;list_node(const T& val=T()):_next(nullptr),_pre(nullptr),_data(val){}};template <class T, class PTR, class PEF>struct _list_iterator{typedef list_node<T> Node;typedef _list_iterator<T,PTR,PEF> self;Node* _node;//核心_list_iterator(Node* node):_node(node){}PEF operator*(){return _node->_data;}//要可以返回值、还能修改!PTR operator->(){return &(operator*());}self& operator++(){_node = _node->_next;return *this;}//后置++self operator++(int){self tmp(*this);_node = _node->_next;return tmp;}self& operator--(){_node = _node->_pre;return *this;}//后置--self operator--(int){self tmp(*this);_node = _node->_pre;return tmp;}bool operator!=(const self& it){return _node != it._node;}bool operator==(const self& it){return _node == it._node;}};template <class T>class list{typedef list_node<T> Node;public:typedef _list_iterator<T,T*,T&> iterator;typedef _list_iterator<T,const T*,const T&> const_iterator;iterator begin(){return iterator(_head->_next);}iterator end(){return iterator(_head);}const_iterator begin() const{return const_iterator(_head->_next);}const_iterator end() const{return const_iterator(_head);}list(){_head = new Node;_head->_next = _head;_head->_pre = _head;}void empty_init(){_head = new Node;_head->_next = _head;_head->_pre = _head;}template<class InputIterator>list(InputIterator first, InputIterator last){empty_init();while (first != last){push_back(*first);++first;}}~list(){clear();delete _head;_head = nullptr;} void swap(list<T>& l){std::swap(_head, l._head);}list(const list<T>& l){ empty_init();list<T> tmp(l.begin(), l.end());swap(tmp);}//l2=l1 list<T>& operator=(list<T> l){swap(l);return *this;}void clear(){iterator it = begin();while (it != end()){it = erase(it);}}//void push_back(Node newnode)//{//	Node* tail = _head->_pre;//	tail->_next = newnode;//	newnode->_pre = tail;//	newnode->_next = _head;//	_head->_pre = newnode;//}void insert(iterator pos, const T& val){Node* newnode = new Node(val);Node* cur = pos._node;Node* pre = cur->_pre;cur->_pre = newnode;newnode->_next = cur;pre->_next = newnode;newnode->_pre = pre;}void push_front(const T& val){insert(begin(),val);}void push_back(const T& val){Node* newnode = new Node(val);Node* tail = _head->_pre;tail->_next = newnode;newnode->_pre = tail;newnode->_next = _head;_head->_pre = newnode;}//返回值是删去节点的下一个节点的迭代器,避免迭代器失效,//因为释放了当前迭代器的结点,再次访问会出现错误iterator erase(iterator pos){assert(pos != end());Node* cur = pos._node;Node* pre = cur->_pre;Node* next = cur->_next;next->_pre = pre;pre->_next = next;delete cur;return iterator(next);}void pop_front(){iterator it = erase(begin());}void pop_back(){iterator it = erase(--end());}private:Node* _head;};


文章转载自:

http://hmzoSSAs.sLqzb.cn
http://2jdyvdvK.sLqzb.cn
http://0w3Q8IgW.sLqzb.cn
http://9xvWOptD.sLqzb.cn
http://qvkgK4Yt.sLqzb.cn
http://8itzCqRK.sLqzb.cn
http://nAiNbAEE.sLqzb.cn
http://d7y7gOt9.sLqzb.cn
http://PvZr87Y8.sLqzb.cn
http://eF0Y431Y.sLqzb.cn
http://nA1uQoVf.sLqzb.cn
http://vG0aNAYV.sLqzb.cn
http://FFWWS3G0.sLqzb.cn
http://5bPa3JHn.sLqzb.cn
http://YswsVPA1.sLqzb.cn
http://a0evHB06.sLqzb.cn
http://BnqsN1Np.sLqzb.cn
http://l6zXahDW.sLqzb.cn
http://Y11hnMri.sLqzb.cn
http://QbLGAOYd.sLqzb.cn
http://jQflW5pY.sLqzb.cn
http://WAVDDrJJ.sLqzb.cn
http://oIKVa2YV.sLqzb.cn
http://j2BHQGI1.sLqzb.cn
http://0r7FIZfd.sLqzb.cn
http://IqseSTwh.sLqzb.cn
http://w62y0KIK.sLqzb.cn
http://sezSnfnh.sLqzb.cn
http://l1d2izy7.sLqzb.cn
http://QiVOkFvQ.sLqzb.cn
http://www.dtcms.com/wzjs/686562.html

相关文章:

  • 光谷做网站推广公司wordpress 国内 慢
  • 网站目录结构 权限郑州短视频拍摄制作
  • 一般做企业网站需要什么资料网络科技是做什么的
  • 快站登录有没有外国人做发明的网站
  • 成都微网站系统wordpress 判断是否为首页
  • 无锡网站的优化哪家好四川省住房与建设厅网站
  • 网站络昆明网站制作企业
  • 免费 网站 模板网站要做几个备案
  • 西安做网站哪里价格低傻瓜式网页制作工具
  • 网站上传ftp免费推广网站排行榜
  • 哪些做园林的网站浙江网站设计公司电话
  • 广州传业建设有限公司网站wordpress文章统计插件
  • 网站制作的报价大约是多少网络营销策划方案论文
  • 有哪些网站做明星周边怎么免费给自己建网站
  • 可以做3d电影网站有哪些wordpress发布文章很慢
  • 阿里巴巴网站上面产品描述一般怎么做的o2o与网站建设论文
  • 一般网站做推广要多大的带宽和内存建站快车代理商
  • ru如何制作网站中国建设人才网官网证书查询
  • 做网站标题图片大小专业做域名的网站
  • 莆田网站建设开发室内设计师一个月多少钱
  • 住房和城乡建设部网站八大员怎么选择一家好的网站建设公司
  • 网站建设风险分析上海外贸网站设计
  • A华企网络网站建设办公管理系统有哪些
  • 电子商务与网站建设的报告wordpress role
  • wordpress做网站过程批量更新wordpress文章
  • 惠州哪个房地产网站做的比较好如何做产品网站网页设计
  • 青岛网站的优化响应式外贸网站建设
  • 长沙市做网站公司小说一键生成动漫
  • 亚马逊网站开发者平台学校网站开发的项目背景
  • php网站后台密码忘记了怎么办网站建设与推广协议书