您的位置:首页 > 娱乐 > 明星 > 设计公司的企业文化内容_网页图片加载失败_广州百度快速排名优化_在线服务器网站

设计公司的企业文化内容_网页图片加载失败_广州百度快速排名优化_在线服务器网站

2025/4/19 7:38:30 来源:https://blog.csdn.net/2403_87752060/article/details/147315816  浏览:    关键词:设计公司的企业文化内容_网页图片加载失败_广州百度快速排名优化_在线服务器网站
设计公司的企业文化内容_网页图片加载失败_广州百度快速排名优化_在线服务器网站

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;};

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com