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

百度提交网站的入口地址做婚纱网站的步骤

百度提交网站的入口地址,做婚纱网站的步骤,中国空间站搭建国际合作平台,定制vx免费1&#xff1a;list的模拟实现 1&#xff1a;链表的节点 对于list的模拟实现&#xff0c;我们需要先定义一个节点的类可以使用&#xff08;class也可以使用struct&#xff09; // List的节点类 template<class T> struct ListNode {ListNode(const T& val T()){_p…

1:list的模拟实现

1:链表的节点

对于list的模拟实现,我们需要先定义一个节点的类可以使用(class也可以使用struct)

// List的节点类
template<class T>
struct ListNode
{ListNode(const T& val = T()){_pPre = nullptr;_pNext = nullptr;_val = val;}ListNode<T>* _pPre;ListNode<T>* _pNext;T _val;
};

上面的结构体和我们模拟实现链表的代码基本上差不多,只不过将初始化化成了构造函数,并且将链表封装成一个类并且提供对于链表的操作。

2:链表的迭代器

为什么我们现在就需要学习链表的迭代器,那是因为除了我们在容器外使用迭代器,我们链表容器内部本身也使用迭代器完成很多操作。

//List的迭代器类
template<class T, class Ref, class Ptr>
//T是节点储存的数据类型,Ref是T的引用T&,Ptr是T的指针T*
struct ListIterator
{typedef ListNode<T>* PNode;typedef ListIterator<T, Ref, Ptr> Self;//注意Self的重命名是定义的迭代器自己typedef Ref reference; //为反向迭代器做铺垫 typedef Ptr pointer;//为反向迭代器做铺垫ListIterator(PNode pNode = nullptr) : _pNode(pNode) {}ListIterator(const Self& l) :_pNode(l._pNode) {}T& operator*(){return _pNode->_val;}T* operator->(){return &(_pNode->_val);}Self& operator++(){_pNode = _pNode->_pNext;return *this;}Self operator++(int){Self tmp(_pNode);_pNode = _pNode->_pNext;return tmp;}Self& operator--(){_pNode = _pNode->_pPre;return *this;}Self operator--(int){Self tmp(_pNode);_pNode = _pNode->_pPre;return tmp;}bool operator!=(const Self& l) const{return _pNode != l._pNode;}bool operator==(const Self& l) const{return _pNode == l._pNode;        }PNode _pNode;
};

为什么提供了三个模版参数,因为在对于迭代器自己操作中,可能需要返回T的引用或者T的地址,比如*和->的运算符重载。

在迭代器里面,本质上就是定义一个ListNode*<T> 的一个指针,来对链表进行操作。

3:链表的增删查改

template<class T>
class list
{typedef ListNode<T> Node;typedef Node* PNode;
public:typedef ListIterator<T, T&, T*> iterator;typedef ListIterator<T, const T&, const T*> const_iterator;typedef Reverse_iterator<iterator> reverse_iterator;//反向迭代器typedef Reverse_iterator<const_iterator> const_reverse_iterator;//反向迭代器
public:///// List的构造list(){CreateHead();}list(int n, const T& value = T()){CreateHead();for (int i = 0; i < n; i++){push_back(value);}}template <class Iterator>list(Iterator first, Iterator last){CreateHead();while (first != last){push_back(*first);first++;}}list(const list<T>& l){CreateHead();list<T> tmp(l.begin(), l.end());swap(tmp);}list<T>& operator=(const list<T> l){swap(l);return *this;}~list(){clear();delete _pHead;_pHead = nullptr;}///// List Iteratoriterator begin(){return iterator(_pHead->_pNext);}iterator end(){return iterator(_pHead);}const_iterator begin() const{return const_iterator(_pHead->_pNext);}const_iterator end() const{return const_iterator(_pHead);}reverse_iterator rbegin(){return reverse_iterator(end());}reverse_iterator rend(){return reverse_iterator(begin());}///// List Capacitysize_t size()const{auto it = begin();size_t count = 0;while (it != end()){it++;count++;}return count;}bool empty()const{return _pHead->_pNext == _pHead;}// List AccessT& front(){return _pHead->_pNext->_val;}const T& front()const{return _pHead->_pNext->_val;}T& back(){return _pHead->_pPre->_val;}const T& back()const{return _pHead->_pPre->_val;}// List Modifyvoid push_back(const T& val){ insert(end(), val);}void pop_back(){erase(--end()); }void push_front(const T& val) { insert(begin(), val); }void pop_front(){ erase(begin()); }// 在pos位置前插入值为val的节点iterator insert(iterator pos, const T& val){Node* newnode = new Node(val);Node* pcur = pos._pNode;newnode->_pPre = pcur->_pPre;newnode->_pNext = pcur;pcur->_pPre->_pNext = newnode;pcur->_pPre = newnode;return iterator(newnode);}// 删除pos位置的节点,返回该节点的下一个位置iterator erase(iterator pos){        assert(size()>0);       Node* pcur = pos._pNode;Node* pret = pcur->_pNext;pcur->_pPre->_pNext = pcur->_pNext;pcur->_pNext->_pPre = pcur->_pPre;delete pcur;return iterator(pret);}void clear(){Node* cur = _pHead->_pNext;while (cur != _pHead){_pHead->_pNext = cur->_pNext;delete cur;cur = _pHead->_pNext;}_pHead->_pNext = _pHead->_pPre = _pHead;}void swap(list<T>& l){std::swap(_pHead, l._pHead);}
private:void CreateHead(){_pHead = new ListNode<T>; //这里是模版_pHead->_pPre = _pHead;_pHead->_pNext = _pHead;}PNode _pHead;
};
1:list的构造

对于list的构造我们实现了四种构造方式,第一是直接构造一个空链表,第二是使用n个相同元素构造链表,第三是使用迭代器来构造链表,第四就是使用list本身构造链表,额外重载运算符=来实现链表。

2:list的迭代器在类中的返回

我们可以很直观的看到迭代器在类中是返回的什么。

3:list的容量判断

我们之间在类的内部使用迭代器便利链表来计算链表大小。

4:增删操作

逻辑和以前对于链表的实现上大型不差,出了额外增加了几个接口然后使用迭代器。

2:反向迭代器的实现

反向迭代器本质上就是正向迭代器的封装

 template<class Iterator>struct Reverse_iterator{public:
// 注意:此处typename的作用是明确告诉编译器,Ref是Iterator类中的类型,而不是静态成员变量
// 否则编译器编译时就不知道Ref是Iterator中的类型还是静态成员变量
// 因为静态成员变量也是按照 类名::静态成员变量名 的方式访问的typedef typename Iterator::reference Ref;  // 从正向迭代器提取typedef typename Iterator::pointer Ptr;typedef Reverse_iterator<Iterator> Self;public:Reverse_iterator(Iterator it = nullptr) :_it(it) {}Ref operator*(){Iterator temp(_it);--temp;return *temp;}Ptr operator->(){return &(operator*());}Self operator++(){--_it;return *this;}Self operator++(int){Self temp(*this);--_it;return temp;}Self operator--(){++_it;return *this;}Self operator--(int){Self temp(*this);++_it;return temp;}bool operator!=(const Self& l)const{return _it != l._it;}bool operator==(const Self& l)const{return _it == l._it;}Iterator _it;};


文章转载自:

http://50KsxbnV.zqypz.cn
http://Cyq34UaA.zqypz.cn
http://sdjHnWFZ.zqypz.cn
http://e6rC3GUk.zqypz.cn
http://4Evk3rip.zqypz.cn
http://ZqnSZq2m.zqypz.cn
http://YjmXQxJw.zqypz.cn
http://kWjB1xTN.zqypz.cn
http://iY9bSLZl.zqypz.cn
http://aJ37gqIk.zqypz.cn
http://qrES6HOZ.zqypz.cn
http://OOe1LXG2.zqypz.cn
http://GmrpLlQT.zqypz.cn
http://iO2Hu3GS.zqypz.cn
http://sM3POKvm.zqypz.cn
http://r2VuZHhM.zqypz.cn
http://DUz8OcFX.zqypz.cn
http://d3zCApQl.zqypz.cn
http://xWoDss4O.zqypz.cn
http://eBgq4WkY.zqypz.cn
http://M5vlu0Uu.zqypz.cn
http://amVEPWTb.zqypz.cn
http://ok6DNoWn.zqypz.cn
http://lnGF4Nbz.zqypz.cn
http://ErYxKjcw.zqypz.cn
http://QD3vQbmb.zqypz.cn
http://aGPaJMwC.zqypz.cn
http://BkOx6jUm.zqypz.cn
http://5gCiu3FD.zqypz.cn
http://5z1Po841.zqypz.cn
http://www.dtcms.com/wzjs/632859.html

相关文章:

  • 公司网站开发说明介绍wordpress 前端 修改
  • 网站开发教程公司手机网站排名优化
  • 软文推广网站做网站有多赚钱
  • 建立手机也可浏览的网站教程中国铁建网站
  • 合肥网站建设步骤正规网站备案代理
  • 竹子建站怎么赚钱wordpress公众号验证码
  • 邢台手机网站建设价格成都搜索优化整站优化
  • 手机网站弹出层插件有哪些网页设计中所需要的素材
  • 专业网站设计建设服务wordpress简洁设置
  • 苏宁网站建设房产网排名
  • 帝国cms网站搬家教程龙口市最新公告
  • 创美艺佳网站是谁做的域名解析到本地服务器
  • 国外做化工网站惠州市建设厅网站
  • 网站建设市场需求分析做运营需要知道素材网站
  • 百度搜索网站淘宝网店开店网站建设
  • 现在做个人网站管理咨询顾问是做什么的
  • 什么系统做购物网站好临汾网站开发
  • 石家庄网站建设模板网站所有页面只显示域名
  • 金华建站模板UE4做购物网站
  • 自营店网站建设html5移动网站制作
  • 网站建设培训哪里好网页游戏排行榜魔域
  • 网站备案单位的联系方式重庆中信建投期货有限公司
  • 沧州网站运营公司中国建设银行下载安装
  • 网站手机自适应cms网站后台模版
  • 自己网站如何做关键词排名阿里云服务器618
  • 在线网站软件免费下载安装贵 建设厅网站文件
  • 锛网站建设部资质网站查询
  • wordpress网站模板下载dede网站幻灯片
  • 有没有专业做淘宝网站吗外贸网络推广专员
  • 建设在线教育网站他达拉非的副作用和危害