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

全国做网站的公黄骅58同城最新招聘信息

全国做网站的公,黄骅58同城最新招聘信息,建筑公司企业文化,传奇手游网页成员变量迭代器(重点)ListIterator运算符重载begin、end 插入、删除inserterase头插、尾插、头删、尾删 operator->const_iterator拷贝构造operator析构函数完整代码 由于前面已经模拟实现了vector,所以这里关于一些函数实现就不会讲的过于…

  • 成员变量
  • 迭代器(重点)
    • ListIterator
    • 运算符重载
    • begin、end
  • 插入、删除
    • insert
    • erase
    • 头插、尾插、头删、尾删
  • operator->
  • const_iterator
  • 拷贝构造
  • operator=
  • 析构函数
  • 完整代码

由于前面已经模拟实现了vector,所以这里关于一些函数实现就不会讲的过于细致。

成员变量

template<class T>
struct ListNode
{ListNode<T>* _next;ListNode<T>* _prev;T _data;ListNode(const T& x = T()):_next(nullptr),_prev(nullptr),_data(x){}
};template<class T>
class list
{typedef ListNode<T> Node;
public:list(){_head = new Node;_head->_next = _head->_prev = _head;_size = 0;}size_t size()const{return _size;}
private:Node* _head;size_t _size;
};

迭代器(重点)

我们想要迭代器能实现以下的功能:

void test_1()
{list<int>lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);lt.push_back(5);list<int>::iterator it = lt.begin();while (it != lt.end()){cout << *it << ' ';++it;}cout << endl;
}

实际上迭代器模拟的是指针的功能,然而链表的指针++并不会得到下一个元素的指针。但我们却希望迭代器++能得到下一个元素的迭代器。

那么我们是不是需要对++进行一个修改,也就是运算符重载。

再仔细想想,重载后的运算符是对这一个类生效的,但我们只需要对迭代器生效。那是不是意味着我们的迭代器需要是一个独立的类:

ListIterator

template<class T>
struct ListIterator
{typedef ListNode<T> Node;typedef ListIterator<T, Ref, Ptr> Self;Node* _node;//内置类型指针ListIterator(Node* node):_node(node){}
}

注意不需要析构函数,实际上我们的迭代器就是原数组结点指针的一个类,怎么可以用过一次后就析构掉呢?

运算符重载

前置++:

Self& operator++()//前置++
{_node = _node->_next;return *this;
}

后置++:

Self operator++(int)//后置++
{Self tmp(*this);//浅拷贝_node = _node->_next;return tmp; 
}

值得注意的是,前后++的重载函数名都是operator++,那么如何区分他们呢?

C++语法规定:后置++的函数参数里面需要有一个int

此外,我这的tmp是一个浅拷贝,这样做有没有问题呢?
我们需要的是指针,那么对于指针自然应该采用浅拷贝。
事实上,我们一般有一个规律:如果类需要重载析构函数,那么一般就要深拷贝,反之一般就需要浅拷贝。

剩余运算符:

Self& operator*()
{return _node->_data;
}Self& operator--()//前置--
{_node = _node->_prev;return *this;
}Self operator--(int)//后置--
{Self tmp(*this);//浅拷贝_node = _node->_prev;return tmp;
}bool operator!=(const Self& it)
{return _node != it._node;
}bool operator==(const Self& it)
{return _node == it._node;
}

begin、end

注意到我们的begin是返回首元素迭代器,而end是返回最后一个元素的下一个位置的迭代器:

typedef ListIterator<T> iterator;
iterator begin() 
{return _head->_next;
}iterator end()
{return iterator(_head);
}

注意到,begin这里我返回的是_head->_next,因为单参数会隐式类型转换:自动调用单参数的构造函数,也就是等价于iterator(_head->_next).

插入、删除

insert

void insert(iterator pos, const T& val)
{Node* cur = pos._node;Node* newnode = new Node(val);Node* prev = cur->_prev;prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;_size++;
}

erase

iterator erase(iterator pos)//返回iterator防止迭代器失效
{Node* cur = pos->_node;Node* prev = cur->_prev;Node* next = cur->_next;prev->_next = next;next->_prev = prev;delete cur;_size--;return iterator(next);
}

值得注意的是,为了防止迭代器失效,我们这里要返回最后一个删除的元素的下一个元素的迭代。

头插、尾插、头删、尾删

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

尾删这里需要注意一定要是end()–,而不能是end()-1.理由很简单,因为我们没有重载-。

operator->

对于非内置类型如下:

struct A
{int _a1;int _a2;A(int a1 = 0, int a2 = 0):_a1(a1),_a2(a2){}
};void test_2()
{list<A>lt;A aa1(1, 1);lt.push_back(aa1);lt.push_back(aa1);lt.push_back(A(2, 2));lt.push_back({ 3,3 });lt.push_back({ 4,4 });list<A>::iterator it = lt.begin();while (it != lt.end()){cout << *it << ' ';//不支持流插入++it;}}

不难发现cout << *it << ’ ';这里会报错,因为自定义类型没有重载<<。
因此我们需要改写为:

cout << (*it)._a1 << ' ' << (*it)._a2 << endl;

不难发现这有些许繁琐,正常来说对于类指针功能我们更希望用->这个操作符,也就是:

cout << it->_a1 << ' ' << it->_a2 << endl;

那就需要重载->:

Self* operator->()
{return &_node->_data;
}

这时候就有人发出疑问了,怎么返回值是一个指针,那要调用_a1,不应该是it->->_a1吗?
没错,这是原生写法,但是我们C++语法给他优化掉了,现在it->_a1等价于it.operator->()->_a1

const_iterator

const_iterator为什么不是const iterator?
这个问题其实前面学习类的时候已经解答过了,事实上我们的const_iterator是不能改变迭代器呢,还是不能改变迭代器指向的元素的值呢?
事实上const_iterator也是可以++的,否则怎么遍历数组元素。
那也就是说const_iterator只是不能改变指向的元素的值,而不是不能改变迭代器本身。

经过上述分析我们得知,我们只需要对*和->重载的返回值作出修改即可:

const T& operator*()
{return _node->_data;
}const T* operator->()
{return &_node->_data;
}

但是,只有返回值不同是无法重载函数的!!
那么我们是不是需要再多写一个const_iterator类呢?
可以,但有更好的方法。
注意到我们只有这两个函数不同,并且只有返回值不同,那是不是可以写一个模板将返回值不同分成不同的类呢?

template<class T,class Ref,class Ptr>
struct ListIterator
{typedef ListNode<T> Node;typedef ListIterator<T, Ref, Ptr> Self;Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}}
typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T, const T&, const T*> const_iterator;const_iterator begin() const
{return _head->_next;
}const_iterator end() const
{return _head;
}

这样我们就省去了CV再写一个类的功夫。

拷贝构造

一样是复用push_back

void empty_init()
{_head = new Node;_head->_next = _head->_prev = _head;_size = 0;
}
list(const T& lt)// 复用push_back来深拷贝
{empty_init();for (auto& e : lt){push_back(e);}
}

operator=

一样是复用拷贝构造:

void swap(list<T>& lt)
{std::swap(_head, lt._head);std::swap(_size, lt._size);
}list<T>& operator=(list<T> lt)//传值传参
{swap(lt);return *this;
}

析构函数

同样复用erase

void clear()//没清除头结点
{iterator it = begin();while (it != end()){it = erase(it);}}~list()
{clear();delete _head;_head = nullptr;}

完整代码

template<class T>
struct ListNode
{ListNode<T>* _next;ListNode<T>* _prev;T _data;ListNode(const T& x = T()):_next(nullptr),_prev(nullptr),_data(x){}
};template<class T,class Ref,class Ptr>
struct ListIterator
{typedef ListNode<T> Node;typedef ListIterator<T, Ref, Ptr> Self;Node* _node;//内置类型指针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!=(const Self& it){return _node != it._node;}bool operator==(const Self& it){return _node == it._node;}};template<class T>
class list
{typedef ListNode<T> Node;
public:typedef ListIterator<T, T&, T*> iterator;typedef ListIterator<T, const T&, const T*> const_iterator;iterator begin() //const{return iterator(_head->_next);}iterator end(){return iterator(_head);}//const_iterator而不是const iterator,因为是指向内容不能被修改,而非迭代器本身不能被修改。如果是const iterator就不能it++const_iterator begin() const{return _head->_next;}const_iterator end() const{return _head;}void empty_init(){_head = new Node;_head->_next = _head->_prev = _head;_size = 0;}list(){empty_init();}list(const T& lt)// 复用push_back来深拷贝{empty_init();for (auto& e : lt){push_back(e);}}void swap(list<T>& lt){std::swap(_head, lt._head);std::swap(_size, lt._size);}list<T>& operator=(list<T> lt)//传值传参{swap(lt);return *this;}void clear()//没清除头结点{iterator it = begin();while (it != end()){it = erase(it);}}~list(){clear();delete _head;_head = nullptr;}//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, const T& val){Node* cur = pos._node;Node* newnode = new Node(val);Node* prev = cur->_prev;prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;_size++;}iterator erase(iterator pos)//返回iterator防止迭代器失效{Node* cur = pos->_node;Node* prev = cur->_prev;Node* next = cur->_next;prev->_next = next;next->_prev = prev;delete cur;_size--;return iterator(next);}size_t size()const{return _size;}bool empty()const{return _size == 0;}private:Node* _head;size_t _size;
};

文章转载自:

http://Kzqj0OSl.bhdhL.cn
http://TM8WJCw1.bhdhL.cn
http://KhzRHoM2.bhdhL.cn
http://N8fT1zYI.bhdhL.cn
http://yQB5xajp.bhdhL.cn
http://mpROWboU.bhdhL.cn
http://0vVzMbCw.bhdhL.cn
http://ETODgJOX.bhdhL.cn
http://6SD4R5UK.bhdhL.cn
http://PxNeGFlq.bhdhL.cn
http://9nyGahJJ.bhdhL.cn
http://uH0YDVi0.bhdhL.cn
http://5shfDzOr.bhdhL.cn
http://scviGKDn.bhdhL.cn
http://ivFlJuq4.bhdhL.cn
http://AglPC7jv.bhdhL.cn
http://nUd3fvq8.bhdhL.cn
http://wSCoOLIl.bhdhL.cn
http://l4WgBSlO.bhdhL.cn
http://H0THI2kb.bhdhL.cn
http://W9VfsQhd.bhdhL.cn
http://9UHdSWht.bhdhL.cn
http://8TzSQy21.bhdhL.cn
http://hJMeSK29.bhdhL.cn
http://aYnYHCRK.bhdhL.cn
http://JGmaM3Gx.bhdhL.cn
http://5H4qVWGf.bhdhL.cn
http://70oywdFg.bhdhL.cn
http://UPhyT89x.bhdhL.cn
http://vlrvaHIb.bhdhL.cn
http://www.dtcms.com/wzjs/684527.html

相关文章:

  • 做装修哪个网站推广好库存管理系统软件哪个好
  • 网站建设价格差别鄂尔多斯做网站的公司
  • 网页设计与制作教程杨选辉第四版徐州做网站优化
  • 商务网站建设体会南漳网页设计
  • 谷歌推广网站做的比较漂亮的中国网站
  • 关于工程项目建设的网站wordpress用php版本号
  • 盐城做网站哪家好营口网站制作公司
  • 网站建设蓝图ppt做除尘骨架的网站
  • 免费com域名注册网站星杰设计官网
  • 百度如何搜索到自己的网站wordpress主题修改不了
  • h5响应式网站模板下载怎么编辑网页里面内容
  • 齐河网站建设公司价格企业网站 程序
  • 江苏省建设厅网站资质升级wordpress文章尾部
  • 朱子网站建设网站建设 英语
  • 做360网站官网还是百度知道建设信用卡银行商城网站
  • 英语网站如何做社群事件营销的特点
  • 嘉兴网站seo服务百合怎么doi怎么做网站
  • 南京电器网站建设中山企业网站
  • 做网站的公司如何运营部队网站设计
  • 免费建网站的作用没有服务器怎样做网站
  • 建设网站需要注册证书吗建设网站的合约
  • 临海做网站的公司印花图案设计网站
  • 绵阳网站搜索优化备案号是哪个网站
  • wordpress全站网络工程师 招聘
  • 网站开发技术试验教程wordpress网站打开很慢
  • 建设摩托车官网官方网站自媒体平台注册账号
  • 做网站多少钱 佛山手机端做网站软件
  • 网站制作那家便宜分类网站建设
  • 上海企业建站方案能加速浏览器的加速器
  • 网站实时显示织梦网站首页是哪个文件