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

php网站后台忘记密码推广方案范例

php网站后台忘记密码,推广方案范例,网络工程师的就业前景,最好的网络营销软件目录 list的介绍 list的模拟实现 定义节点 有关遍历的重载运算符 list的操作实现 (1)构造函数 (2)拷贝构造函数 (3)赋值运算符重载函数 (4)析构函数和clear成员函数 (5)尾…

目录

list的介绍 

list的模拟实现

定义节点

有关遍历的重载运算符

list的操作实现 

(1)构造函数

 (2)拷贝构造函数

(3)赋值运算符重载函数=

(4)析构函数和clear成员函数

(5)尾插/头插和尾删/头删

(6)size成员函数

(7)在任意位置插入 (insert)

(8)任意位置删除(erase)

(9)迭代器 

完整代码展示

vector和list的比较

1.排序

(1)list和vector排序

(2)list copy vector sort copy list sort和list

2.总结

 

list的介绍 

 (1)list类其实就是链表,但是它是双向链表。在数据结构中我们了解过双向链表的特点。下面我们回忆一下。

1.节点中具有两个指针。一个指针指向该节点的前一个节点,另一个指针指向该节点的下一个节点。

2.存在哨兵位。初始化的时候节点里的下一个节点和上一个节点都指向自己。

 (2)STL中list的底层结构

 list的模拟实现

定义节点

我们先定义双向链表的节点并初始化。

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

有关遍历的重载运算符

list容器有迭代器,那么就可以进行遍历,因此我们要可以++,--等运算符重载。而且在插入删除操作中我们常常需要 ‘.’    '->'对链表进行遍历。因为普通迭代器和const迭代器中只有operator*和operator->的返回值有区别,所以我们就在模板上多增加了两个模板参数。

代码如下: 

template<class T,class Ref,class Ptr>struct list_iterator{typedef list_node<T> Node;typedef list_iterator<T, Ref, Ptr> Self;Node* _node;list_iterator(Node* node):_node(node){}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}//C++规定后缀调用需要有一个int型参数作为区分前缀与后缀调用的区别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& lt){return _node == lt._node;//结构体变量用「.」来访问成员,而结构体指针用「->」来访问。}bool operator!=(const Self& lt){return _node != lt._node;}};

list的操作实现 

(1)构造函数

创建节点并把节点中的指针全部指向自己。

list()
{
_head = new node;
_head->_next = _head;
_head->_prev = _head;
}

 (2)拷贝构造函数

先构造a1,再把lt中的资源尾插给a1。

      void empty_Init(){_head = new node;_head->_next = _head;_head->_prev = _head;}   //a1(a2),a1是新建的list(const list<T>& lt){empty_Init();for (auto& e : lt){push_back(e);}}//initializer_list<T>list(std::initializer_list<T> lt){empty_Init();for (auto& e : lt){push_back(e);}}

两种拷贝构造的区别: 

(3)赋值运算符重载函数=

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

 看到这个代码我们就会想为什么运算符重载中的形参不加const呢?如果加了const就像 void swap(*this,const list<T>& y)一样,这样是会报错的,两边类型不同,swap函数是一个函数模板,只有一个模板参数,那么有人会说把这个改成类型相同的不就行了。但是我们知道const list<T>& lt中const修饰list<T> 类型,则lt 引用的对象(即 list<T>)是常量对象,不能通过 lt 修改它的内容。它的值在初始化后就不能改变,而在swap函数中需要交换它们的资源,那么lt就需要改变。

(4)析构函数和clear成员函数

clear的作用只是清理链表的节点,只剩下哨兵位,并不会释放空间。

       ~list(){clear();delete _head;_head = nullptr;}void clear(){iterator it = begin();while (it != end()){it = erase(it);}}

(5)尾插/头插和尾删/头删

        void push_back(const T&x){insert(end(), x);}void push_front(const T&x){insert(begin(), x);}void pop_back(){erase(--end());//end()是哨兵位}void pop_front(){erase(begin());}

(6)size成员函数

        size_t size()const{return _size;}

(7)在任意位置插入 (insert)

         typedef list_iterator<T,T&,T*> iterator;   void insert(iterator pos, const T& x){node* newnode = new node(x);node*pre = pos._node;node* prev = pre->_prev;prev->_next = newnode;newnode->_prev = prev;newnode->_next = pre;pre->_prev = newnode;_size++;}

注意:pos的类型是list_iterator<T,T&,T*>,这个类中的成员变量只有_node,而_node的类型才是list_node,类型为list_node才有节点的成员变量。所以我们要node*pre = pos._node,而不能直接使用pos。

(8)任意位置删除(erase)

list中的erase也会有迭代器失效,所以我们需要返回下一个迭代器。

         typedef list_iterator<T,T&,T*> iterator; iterator erase(iterator pos){assert(pos != end());node* pre = pos._node;node* prev = pre->_prev;node* next = pre->_next;delete pre;prev->_next = next;next->_prev = prev;_size--;return iterator(next);}

(9)迭代器 

        iterator begin(){iterator it(_head->_next);//调用了list_iterator类模板的构造函数return it;}iterator end(){iterator it(_head);return it;}const_iterator begin()const{const_iterator it(_head->_next);return it;}const_iterator end()const{const_iterator it(_head);return it;}

完整代码展示

#include<assert.h>
namespace slm
{//创建节点template <class T>struct list_node{list_node* _next;list_node* _prev;T _data;list_node(const T& x = T()):_next(nullptr), _prev(nullptr), _data(x){}};//实现运算符重载template<class T,class Ref,class Ptr>//template<class T,class T&,class T*>struct list_iterator{typedef list_node<T> Node;typedef list_iterator<T, Ref, Ptr> Self;Node* _node;list_iterator(Node* node):_node(node){}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}//C++规定后缀调用需要有一个int型参数作为区分前缀与后缀调用的区别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& lt){return _node == lt._node;//结构体变量用「.」来访问成员,而结构体指针用「->」来访问。}bool operator!=(const Self& lt){return _node != lt._node;}};template<class T>class list{typedef list_node<T> node;typedef list_iterator<T,T&,T*> iterator;typedef list_iterator<T,const T&,const T*> const_iterator;public:iterator begin(){iterator it(_head->_next);return it;}iterator end(){iterator it(_head);return it;}const_iterator begin()const{const_iterator it(_head->_next);return it;}const_iterator end()const{const_iterator it(_head);return it;}void empty_Init(){_head = new node;_head->_next = _head;_head->_prev = _head;}list(){empty_Init();}//a1(a2)list(const list<T>& lt){empty_Init();for (auto& e : lt){push_back(e);}}list(std::initializer_list<T> lt){empty_Init();for (auto& e : lt){push_back(e);}}void swap(list<T>& lt){std::swap(_head, lt._head);std::swap(_size, lt._size);}//lt1=ltlist<T>& operator=(list<T>& lt){swap(lt);return *this;}~list(){clear();delete _head;_head = nullptr;}void clear(){iterator it = begin();while (it != end()){it = erase(it);}}size_t size()const{return _size;}void push_back(const T&x){insert(end(), x);}//在pos前插入void insert(iterator pos, const T& x){node* newnode = new node(x);node*pre = pos._node;node* prev = pre->_prev;prev->_next = newnode;newnode->_prev = prev;newnode->_next = pre;pre->_prev = newnode;_size++;}iterator erase(iterator pos){assert(pos != end());node* pre = pos._node;node* prev = pre->_prev;node* next = pre->_next;delete pre;prev->_next = next;next->_prev = prev;_size--;return iterator(next);}void push_front(const T&x){insert(begin(), x);}void pop_back(){erase(--end());//end()是哨兵位}void pop_front(){erase(begin());}private:node* _head;size_t _size;};
}

vector和list的比较

1.排序

(1)list和vector排序

#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
using namespace std;
void test_op1()
{srand(time(0));const int N = 1000000;list<int> lt1;vector<int> v;for (int i = 0; i < N; ++i){auto e = rand() + i;lt1.push_back(e);v.push_back(e);}int begin1 = clock();// vector排序sort(v.begin(), v.end());int end1 = clock();//list排序int begin2 = clock();lt1.sort();int end2 = clock();printf("vector sort:%d\n", end1 - begin1);printf("list sort:%d\n", end2 - begin2);
}
int main()
{
test_op1();
return 0;
}

从结果中我们发现list排序比vector排序快了两倍多。

(2)list copy vector sort copy list sort和list

那如果我们先把list类资源拷贝构造给vector排序,排完序后又拷贝回list类,那结果会是如何呢?

#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
using namespace std;
void test_op2()
{srand(time(0));const int N = 10000;list<int> lt1;list<int> lt2;for (int i = 0; i < N; ++i){auto e = rand() + i;lt1.push_back(e);lt2.push_back(e);}int begin1 = clock();// 拷贝vectorvector<int> v(lt2.begin(), lt2.end());// 排序sort(v.begin(), v.end());// 拷贝回lt2lt2.assign(v.begin(), v.end());int end1 = clock();int begin2 = clock();lt1.sort();int end2 = clock();printf("list copy vector sort copy list sort:%d\n", end1 - begin1);printf("list sort:%d\n", end2 - begin2);
}int main()
{test_op2();return 0;
}

我们发现上面list先拷贝构造成vector类在排序,排完序后再拷贝回list的效率还是比直接list排序慢。

2.总结

 vectorlist



动态顺序表,一段连续空间带头结点的双向循环链表


访
支持随机访问,访问某个元素效率O(1)不支持随机访问,访问某个元
素效率O(N)




任意位置插入和删除效率低,需要搬移元素,时间
复杂度为O(N),插入时有可能需要增容,增容:
开辟新空间,拷贝元素,释放旧空间,导致效率更

任意位置插入和删除效率高,
不需要搬移元素,时间复杂度
为O(1)





 
底层为连续空间,不容易造成内存碎片,空间利用
率高,缓存利用率高
底层节点动态开辟,小节点容
易造成内存碎片,空间利用率
低,缓存利用率低




 
原生态指针对原生态指针(节点指针)进行
封装




在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效.

删除时,当前迭代器需要重新赋值否则会失效

插入元素不会导致迭代器失
效,删除元素时,只会导致当
前迭代器失效,其他迭代器不
受影响
使


需要高效存储,支持随机访问,不关心插入删除效率大量插入和删除操作,不关心
随机访问

 

 

 

 


文章转载自:

http://ZHuTWaWe.snnfn.cn
http://Fp9Q9RDC.snnfn.cn
http://7W1IVQkF.snnfn.cn
http://XKbyYSTy.snnfn.cn
http://bBLPsIOy.snnfn.cn
http://psy4vxs7.snnfn.cn
http://0iS0b5Ef.snnfn.cn
http://QJkCTBDk.snnfn.cn
http://eVk5Ycdr.snnfn.cn
http://abFC2cSH.snnfn.cn
http://xI7QwIhw.snnfn.cn
http://TZlWVbSS.snnfn.cn
http://lCOINPpC.snnfn.cn
http://ut83mqlV.snnfn.cn
http://2hlaBsfE.snnfn.cn
http://q3AcJDB5.snnfn.cn
http://afEXuxhC.snnfn.cn
http://QbMvwGsy.snnfn.cn
http://MS4iSAd2.snnfn.cn
http://mzSGjl2T.snnfn.cn
http://YM0bUsKP.snnfn.cn
http://dwfkoYYl.snnfn.cn
http://W70sHt13.snnfn.cn
http://VSlB5BnA.snnfn.cn
http://Qeb5mMat.snnfn.cn
http://Jar1xCAU.snnfn.cn
http://7j8pmgrJ.snnfn.cn
http://zqjgaOvO.snnfn.cn
http://oXoqKutn.snnfn.cn
http://FQCBiaHt.snnfn.cn
http://www.dtcms.com/wzjs/700136.html

相关文章:

  • 网站建设标语文案海誉网站定制
  • 做网站和app需要多久为什么很少人敢娶外贸女
  • 上海做网站企业wordpress支付宝付费
  • 苏州高端网站建设如何更改WordPress登录密码
  • 企业网站可以免费做吗网站渗透案例
  • 广州市外贸网站建设作文网课哪家好
  • 常州网站建设解决方案手机网站demo
  • 手机微网站怎么制作90设计网站兼职怎么样
  • 哪些网上订餐的网站做的好网站实名认证需要什么资料
  • 医疗网站建设咨询中山里水网站建设
  • 网站配色案例wordpress新建页面没有模板
  • 宝坻做网站哪家好专业的企业网站建设
  • 杭州网企业网站建设濮阳市城乡一体化示范区开州街道
  • 宏润建设网站wordpress响应式图片主题
  • 临沂企业做网站做3d ppt模板下载网站
  • 网站建设优化服务效果海外互联网推广平台
  • 网站开发维护印花税影视广告公司宣传片
  • 购物网站建设容易出现的问题潍坊网站建设诸城高密
  • 云南旅游网站开发公司网址大全
  • 扬州 网站建设网店推广1+x证书
  • 免费财务软件永久版百度seo推广方案
  • 网站建设 html5做招聘网站创业
  • 珠宝玉器监测网站建设方案来宾住房与城乡建设网站
  • 网站建设制作作业wordpress更新php版本号
  • 网站空间后台求职简历网
  • 婚庆素材网站免费网站视频播放代码
  • 玉溪建设网站百姓网站制作
  • 电子商务系统网站设计wordpress修改固定链接后无法访问
  • 深圳市企业网站建设哪家好汽车网站建设方案预算
  • 国外创意网站设计欣赏开发帮官方网站