工信部信息备案网站首页wordpress 多站点 合集
news/
2025/10/7 21:54:11/
文章来源:
工信部信息备案网站首页,wordpress 多站点 合集,万网站建设,wordpress 头部空白目录 前提#xff1a;list 的基本介绍 一、构造/析构/拷贝/赋值
1、构造函数
2、析构函数
3、拷贝构造函数
4、赋值
二、修改操作
1、push_back
2、insert
3、erase
4、clear
三、list iterator 的使用
1、operator *
2、operator
3、operator -- 4、operator list 的基本介绍 一、构造/析构/拷贝/赋值
1、构造函数
2、析构函数
3、拷贝构造函数
4、赋值
二、修改操作
1、push_back
2、insert
3、erase
4、clear
三、list iterator 的使用
1、operator *
2、operator
3、operator -- 4、operator
5、operator 6、begin( ) / end( )
四、迭代器模板参数 1、const_iterator (重要) 2、operator-重要 五、完整代码 前提list 的基本介绍
【list的文本介绍】 ⭕list是可以在常数范围内在任意位置进行插入和删除的序列式容器并且该容器可以前后双向迭代。 ⭕ list的底层是双向链表结构双向链表中每个元素存储在互不相关的独立节点中在节点中通过指针指向其前一个元素和后一个元素。 ⭕ list与 forward_list 非常相似最主要的不同在于 forward_list 是单链表只能朝前迭代已让其更简单高效。 ⭕与其他的序列式容器相比(arrayvectordeque)list通常在任意位置进行插入、移除元素的执行效率更好。 ⭕ 与其他序列式容器相比list 和 forward_list 最大的缺陷是不支持任意位置的随机访问比如要访问list的第6个元素必须从已知的位置(比如头部或者尾部)迭代到该位置在这段位置上迭代需要线性的时间开销list还需要一些额外的空间以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素) 一、构造/析构/拷贝/赋值 我们已经知道 list 的底层逻辑是双向链表结构所以 list 定义模版参数为T并且节点结构体中有两个指针_next、_prev)。使用 struct 类来定义节点是为了便于 list 类访问因为 struct 不对成员的访问进行限制。
1、构造函数
链表是由一个个的结点连接而成所以第一步先构造出一个结点类。这个结点包含前后指针和对应的数据。对这个节点也需要进行初始化。 2、析构函数
可以调用 clear 函数然后把 head 删除 3、拷贝构造函数 4、赋值 二、修改操作
1、push_back 功能尾插一个节点。 步骤利用 head 将最后一个节点记为 tail 创建一个新的节点 newnode 更新节点的 next 和 prev 2、insert 功能在 pos 处插入一个新的节点。 步骤记录 pos 和 pos 前一个位置的值。 新建一个新的节点。 更新节点的 prev 和 next。 insert 也可以帮助尾插和头插 3、erase 功能删除 pos 处的节点。 要注意迭代器失效的问题。具体内容可以参考【迭代器失效】 erase 也可以帮助尾删和头删 4、clear 功能清除所有节点但是保留头结点。 三、list iterator 的使用 list 容器的迭代器不再是像 string 或者 vector 那样的原生指针了首先因为 list 的各个节点在物理空间上不是连续的我们不能直接对节点的地址进行 / - - 得到其前、后位置的迭代器并且我们的数据是保存在节点之中的不能把节点的指针解引用直接得到里面数据。这些操作我们可以通过封装节点的地址形成一个迭代器类然后重载这个类的 operator* 和 operator 等运算符及其他一系列方法最终可以向操作指针一样去操作迭代器。 1、operator * 功能获取该迭代器指向位置的值 2、operator 功能迭代器向后移动一位。返回值依旧是迭代器 3、operator -- 功能迭代器向前移动一位 4、operator 功能判断节点的指针是否不等。 5、operator 功能判断节点的指针是否相等。 6、begin( ) / end( )
begin () 和 end() 的 const 版本需要借助 const 迭代器。 四、迭代器模板参数 1、const_iterator (重要)
在上述 begin 和 end 的函数中const 修饰的是迭代器本身并不是迭代器指向的内容导致迭代器所指向的内容依旧可以修改。但是我们使用 const 修饰时希望达成的目标是不能改变迭代器所指向的内容迭代器自己可以修改。所以重定义一个 const 迭代器模板。 虽然重新定义一个 const 版本的迭代器可以使代码正常运行但是代码显得很冗余只有解引用处会不一样。为了解决这个问题我们可以在定义迭代器模板时增加一个参数模板参数不同就可以实例化为两个不同的类这样当传 const 修饰的值时就会调用 const 迭代器。 2、operator-重要 功能迭代器是模拟指针的行为通过-来访问迭代器所指向的内容。 这里我们可以像 const 迭代器一样增加一个类参数来应对const迭代器而设计的只不过这个模板参数控制的是箭头运算符重载函数的返回值类型因为const迭代器使用 -运算符后返回的应该是const T类型而正常迭代器使用 -运算符后返回的应该是T。 【补充】为什么要重载 -
我们知道想调用成员可以使用 . 和 -。下面箭头运算符其实是省略了一个-it-_data 首先调用重载函数 it.operator-(). 返回的T* ,那 T* 是怎么访问的 _data 的呢这里明显少了一个箭头正确的写法应该是这样it--_data 但编译器为了简洁好看将一个箭头省略了。 五、完整代码
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#includeassert.h
#includeiostreamnamespace zhou
{templateclass Tstruct List_node{//List_node类名List_nodeT类型List_nodeT* _next;//指向后一个节点List_nodeT* _prev;//指向前一个节点T _data;//存储的数据//构造函数//T()List_node(const T x T()):_next(nullptr), _prev(nullptr),_data(x){}};//迭代器的类templateclass T,class Ref,class Ptrstruct _list_iterator{typedef List_nodeT node;typedef _list_iteratorT,Ref,Ptr self;node* _node;//构造函数_list_iterator(node* n):_node(n){}//解引用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 s){return _node ! s._node;}bool operator(const self s){return _node s._node;}};/*templateclass Tstruct __list_const_iterator{typedef List_nodeT node;typedef __list_const_iteratorT self;node* _node;__list_const_iterator(node* n):_node(n){}const T 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 s){return _node ! s._node;}bool operator(const self s){return _node s._node;}};*/templateclass Tclass list{typedef List_nodeT node;public:typedef _list_iteratorT,T,T* iterator;typedef _list_iteratorT,const T,const T* const_iterator;//typedef __list_const_iteratorT const_iterator;void empty_init(){_head new node;//给头结点创造出一个空间_head-_next _head;_head-_prev _head;//让其头和尾指向自己形成一个循环列表}list(){empty_init();}~list(){clear();delete _head;_head nullptr;}//方法一//list(const listT it)//{// //必须要初始化// empty_init();//将初始化写成一个函数// for (auto e : it)// {// push_back(e);// }//}//方法二void swap(listT tmp){std::swap(_head, tmp._head);}list(const listT lt){empty_init();listT tmp(lt.begin(), lt.end());swap(tmp);}//赋值listT operator(listT lt){swap(lt);return *this;}iterator begin(){/*iterator it(_head-_next);return it;*/return (_head-_next);}iterator end(){return (_head);}const_iterator begin() const{return const_iterator(_head-_next);}const_iterator end() const{return const_iterator(_head);}void push_back(const T x){/*node* tail _head-_prev;node* newnode new node(x);newnode-_next _head;_head-_prev newnode;tail-_next newnode;newnode-_prev tail;*/insert(end(), x);}void push_front(const T x){insert(begin(), x);}void pop_back(){erase(--end());}void pop_front(){erase(begin());}void insert(iterator pos, const T x){node* cur pos._node;node* prev cur-_prev;node* new_node new node(x);prev-_next new_node;new_node-_prev prev;new_node-_next cur;cur-_prev new_node;}iterator erase(iterator pos){assert(pos ! end());node* prev pos._node-_prev;node* next pos._node-_next;prev-_next next;next-_prev prev;delete pos._node;return iterator(next);}void clear(){iterator it begin();while (it ! end()){//it erase(it);erase(it);//erase迭代器返回删除的后一个}}private:node* _head;};void test_list1(){listint It;It.push_back(1);It.push_back(2);It.push_back(3);It.push_back(4);/*listint::iterator It it.begin();while (It ! it.end()){cout (*It) ;It;}*/listint::iterator it It.begin();while (it ! It.end()){(*it) * 2;//修改数据cout *it ;//读取数据it;}cout endl;}struct AA{int _a1;int _a2;AA(int a1 0, int a2 0):_a1(a1),_a2(a2){}};void test_AA(){listAA lt;lt.push_back(AA(1, 1));lt.push_back(AA(2, 2));lt.push_back(AA(3, 3));lt.push_back(AA(4, 4));listAA::iterator it lt.begin();while (it ! lt.end()){cout (*it)._a1 : (*it)._a2 endl;//不习惯使用先解引用后.对象调用成员cout it-_a1 : it-_a2 endl;//-调用迭代器所指向的内容但是需要重载-cout it.operator-()-_a1 : it.operator-()-_a1 endl;//但是编译器会自动省略一个-it;}}}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/930886.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!