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

网站头部样式小程序商城哪家好

网站头部样式,小程序商城哪家好,租域名和服务器要多少钱,营销系统架构一、list的介绍及使用 https://cplusplus.com/reference/list/list/?kwlist list 是可以在常数范围内在任意位置进行插入和删除的序列式容器#xff0c;并且该容器可以前后双向迭代。 list 的底层是双向链表结构#xff0c;双向链表中每个元素存储在互不相关的独立节点中list list 是可以在常数范围内在任意位置进行插入和删除的序列式容器并且该容器可以前后双向迭代。 list 的底层是双向链表结构双向链表中每个元素存储在互不相关的独立节点中在节点中通过指针指向其前一个元素和后一个元素。 list 与 forward_list 非常相似最主要的不同在于 forward_list 是单链表只能朝前迭代不支持尾插、尾删对比双向链表的唯一优势就是每个节点少存一个指针。 与其他的序列式容器相比arrayvectordequelist 通常在任意位置进行插入、移除元素的执行效率更好。 与其他序列式容器相比list 和 forward_list 最大的缺陷是不支持任意位置的随机访问比如要访问 list 的第 6 个元素必须从已知的位置比如头部或者尾部迭代到该位置在这段位置上迭代需要线性的时间开销list 还需要一些额外的空间以保存每个节点的相关联信息对于存储类型较小元素的大 list 来说这可能是一个重要的因素。 1、list的使用 list 中的接口比较多此处类似只需要掌握如何正确的使用然后再去深入研究背后的原理已达到可扩展的能力。以下为list中一些常见的重要接口。 1list的构造 // list的构造 void TestList1() {listint l1; // 构造空的l1listint l2(4, 100); // l2中放4个值为100的元素listint l3(l2.begin(), l2.end()); // 用l2的[begin(), end())左闭右开的区间构造l3listint l4(l3); // 用l3拷贝构造l4// 以数组为迭代器区间构造l5int array[] {16, 2, 77, 29};listint l5(array, array sizeof(array) / sizeof(int));// 列表格式初始化C11listint l6{1, 2, 3, 4, 5};// 用迭代器方式打印l5中的元素listint::iterator it l5.begin();while (it ! l5.end()){cout *it ;it;}cout endl;// C11范围for的方式遍历for (auto e : l5){cout e ;}cout endl; } 2list iterator的使用 可以暂时将迭代器理解成一个指针该指针指向 list 中的某个节点。 // list迭代器的使用 void PrintList(const listint l) {// 注意这里调用的是list的 begin() const返回list的const_iterator对象for (listint::const_iterator it l.begin(); it ! l.end(); it){cout *it ;// *it 10; // 不能改变该值 -- 编译不通过}cout endl; } void TestList2() {int array[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};listint l(array, array sizeof(array) / sizeof(array[0]));// 使用正向迭代器正向list中的元素// listint::iterator it l.begin(); // C98中语法auto it l.begin(); // C11之后推荐写法while (it ! l.end()){cout *it ;it;}cout endl;// 使用反向迭代器逆向打印list中的元素// listint::reverse_iterator rit l.rbegin();auto rit l.rbegin();while (rit ! l.rend()){cout *rit ;rit;}cout endl; } 注意遍历链表只能用迭代器和范围for。 【注意】 begin 与 end 为正向迭代器对迭代器执行操作迭代器向后移动。rbegin(end) 与 rend(begin) 为反向迭代器对迭代器执行操作迭代器向前移动。 3list capacity 4list element access 5list modifiers // list插入和删除 // push_back/pop_back/push_front/pop_front void TestList3() {int array[] {1, 2, 3};listint L(array, array sizeof(array) / sizeof(array[0]));L.push_back(4); // 在list的尾部插入4L.push_front(0); // 在list的头部插入0PrintList(L);L.pop_back(); // 删除list尾部节点L.pop_front(); // 删除list头部节点PrintList(L); } // insert /erase void TestList4() {int array1[] {1, 2, 3};listint L(array1, array1 sizeof(array1) / sizeof(array1[0]));auto pos L.begin(); // 获取链表中第二个节点cout *pos endl;L.insert(pos, 4); // 在pos前插入值为4的元素PrintList(L);L.insert(pos, 5, 5); // 在pos前插入5个值为5的元素PrintList(L);vectorint v{7, 8, 9};L.insert(pos, v.begin(), v.end()); // 在pos前插入[v.begin(), v.end)区间中的元素PrintList(L);L.erase(pos); // 删除pos位置上的元素PrintList(L);L.erase(L.begin(), L.end()); // 删除list中[begin, end)区间中的元素即删除list中的所有元素PrintList(L); } // resize/swap/clear void TestList5() {// 用数组来构造listint array1[] {1, 2, 3};listint l1(array1, array1 sizeof(array1) / sizeof(array1[0]));PrintList(l1);listint l2;l1.swap(l2); // 交换l1和l2中的元素PrintList(l1);PrintList(l2);l2.clear(); // 将l2中的元素清空cout l2.size() endl; } 为什么 C98 建议使用各自容器里的 swap而不建议使用算法里的 swap 可以看到算法里 swap 的 C98 的实现无论是 string、vector、list 使用它会涉及深拷贝问题而且这里的深拷贝代价极大需要深拷贝 3 次 —— 当 l1 和 l2 交换这里会把 l1 拷贝构造一份 c然后把 l2 赋值于 l1c 赋值于 l2完成交换。 而如果是容器里的 swap需要交换 l1 和 l2只需要头指针交换即可。假设是 vector只要把 l1 和 l2 对应的 _start、_finish、_endofstorage 交换即可。相比算法里的 C98 里的 swap这里可以认为没有任何代价。 6list的迭代器失效  先将迭代器暂时理解成类似于指针 迭代器失效即迭代器所指向的节点无效即该节点被删除了 。因为 list 的底层结构为带头结点的双向循环链表因此在 list 中进行 插入 时是 不会 导致 list 的迭代器失效的只有在 删除 时才 会 失效并且 失效的只是指向被删除节点的迭代器 其他迭代器不会受到影响。 void TestListIterator1() {int array[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};listint l(array, arraysizeof(array)/sizeof(array[0]));auto it l.begin();while (it ! l.end()){// erase()函数执行后it所指向的节点已被删除因此it无效在下一次使用it时必须先给其赋值l.erase(it); it;} }// 改正 void TestListIterator() {int array[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};listint l(array, arraysizeof(array)/sizeof(array[0]));auto it l.begin();while (it ! l.end()){l.erase(it); // it l.erase(it);} } ⚪【补充】 容器迭代器的分类 使用功能的角度可分为(正向、反向) const容器底层结构的角度可分为单向、双向、随机 比如单链表迭代器、哈希表迭代器就是单向特征是能 不能 --双向链表迭代器、map 迭代器就是双向特征是能 、–string、vector、deque 迭代器就是随机迭代器特征是能 、–、、-一般随机迭代器底层都是一个连续的空间。 二、list的模拟实现 1、模拟实现list 要模拟实现 list必须要熟悉 list 的底层结构以及其接口的含义。 #pragma once#include iostream using namespace std; #include assert.hnamespace xyl {// List的节点类templateclass Tstruct ListNode{ListNode(const T val T()): _prev(nullptr), _next(nullptr), _val(val){}ListNodeT* _prev;ListNodeT* _next;T _val;};templateclass T, class Ref, class Ptrclass ListIterator{typedef ListNodeT Node;typedef ListIteratorT, Ref, Ptr Self;// Ref 和 Ptr 类型需要重定义下实现反向迭代器的时候需要用到public:typedef Ref Ref;typedef Ptr Ptr;public:// 构造ListIterator(Node* node nullptr): _node(node){}// 具有指针类似行为Ref operator*(){return _node-_val;}Ptr operator-(){return (operator*());}// 迭代器支持移动Self operator(){_node _node-_next;return *this;}Self operator(int){Self temp(*this);_node _node-_next;return temp;}Self operator--(){_node _node-_prev;return *this;}Self operator--(int){Self temp(*this);_node _node-_prev;return temp;}// 迭代器支持比较bool operator!(const Self l)const{return _node ! l._node;}bool operator(const Self l)const{return _node ! l._node;}Node* _node;};templateclass Iteratorclass ReverseListIterator{// 注意这里typename的作用是明确告诉编译器Ref是Iterator类中的一个类型而不是静态成员变量// 否则编译器编译时就不知道Ref是Iterator中的类型还是静态成员变量// 因为静态成员变量也是按照 类名::静态成员变量名 的方式访问的public:typedef typename Iterator::Ref Ref;typedef typename Iterator::Ptr Ptr;typedef ReverseListIteratorIterator Self;public:// 构造ReverseListIterator(Iterator it): _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;};templateclass Tclass list{typedef ListNodeT Node;public:// 正向迭代器typedef ListIteratorT, T, T* iterator;typedef ListIteratorT, const T, const T const_iterator;// 反向迭代器typedef ReverseListIteratoriterator reverse_iterator;typedef ReverseListIteratorconst_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 Iteratorlist(Iterator first, Iterator last){CreateHead();while (first ! last){push_back(*first);first;}}list(const listT l){CreateHead();// 用l中的元素构造临时的temp,然后与当前对象交换listT temp(l.begin(), l.end());this-swap(temp);}listT operator(listT l){this-swap(l);return *this;}~list(){clear();delete _head;_head nullptr;}// List的迭代器iterator begin(){return iterator(_head-_next);}iterator end(){return iterator(_head);}const_iterator begin()const{return const_iterator(_head-_next);}const_iterator end()const{return const_iterator(_head);}reverse_iterator rbegin(){return reverse_iterator(end());}reverse_iterator rend(){return reverse_iterator(begin());}const_reverse_iterator rbegin()const{return const_reverse_iterator(end());}const_reverse_iterator rend()const{return const_reverse_iterator(begin());}// List的容量相关size_t size()const{Node* cur _head-_next;size_t count 0;while (cur ! _head){count;cur cur-_next;}return count;}bool empty()const{return _head-_next _head;}void resize(size_t newsize, const T data T()){size_t oldsize size();if (newsize oldsize){// 有效元素个数减少到newsizewhile (newsize oldsize){pop_back();oldsize--;}}else{while (oldsize newsize){push_back(data);oldsize;}}}// List的元素访问操作// 注意List不支持operator[]T front(){return _head-_next-_val;}const T front()const{return _head-_next-_val;}T back(){return _head-_prev-_val;}const T back()const{return _head-_prev-_val;}// List的插入void push_back(const T val){insert(end(), val);}// List的删除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* pNewNode new Node(val);Node* pCur pos._node;// 先将新节点插入pNewNode-_prev pCur-_prev;pNewNode-_next pCur;pNewNode-_prev-_next pNewNode;pCur-_prev pNewNode;return iterator(pNewNode);}// 删除pos位置的节点返回该节点的下一个位置iterator erase(iterator pos){// 找到待删除的节点Node* pDel pos._node;Node* pRet pDel-_next;// 将该节点从链表中拆下来并删除pDel-_prev-_next pDel-_next;pDel-_next-_prev pDel-_prev;delete pDel;return iterator(pRet);}void clear(){Node* cur _head-_next;// 采用头删除删除while (cur ! _head){_head-_next cur-_next;delete cur;cur _head-_next;}_head-_next _head-_prev _head;}void swap(bite::listT l){std::swap(_head, l._head);}private:void CreateHead(){_head new Node;_head-_prev _head;_head-_next _head;}private:Node* _head;}; }// 对模拟实现的list进行测试 // 正向打印链表 templateclass T void PrintList(const xyl::listT l) {auto it l.begin();while (it ! l.end()){cout *it ;it;}cout endl; }// 测试List的构造 void TestBiteList1() {xyl::listint l1;xyl::listint l2(10, 5);PrintList(l2);int array[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};xyl::listint l3(array, array sizeof(array) / sizeof(array[0]));PrintList(l3);xyl::listint l4(l3);PrintList(l4);l1 l4;PrintList(l1); }// PushBack()/PopBack()/PushFront()/PopFront() void TestBiteList2() {// 测试PushBackxyl::listint l;l.push_back(1);l.push_back(2);l.push_back(3);PrintList(l);// 测试PopBackl.pop_back();l.pop_back();PrintList(l);l.pop_back();cout l.size() endl;// 测试PushFrontl.push_front(1);l.push_front(2);l.push_front(3);PrintList(l);// 测试PopFrontl.pop_front();l.pop_front();PrintList(l);l.pop_front();cout l.size() endl; }// 测试insert和erase void TestBiteList3() {int array[] {1, 2, 3, 4, 5};xyl::listint l(array, array sizeof(array) / sizeof(array[0]));auto pos l.begin();l.insert(l.begin(), 0);PrintList(l);pos;l.insert(pos, 2);PrintList(l);l.erase(l.begin());l.erase(pos);PrintList(l);// pos指向的节点已经被删除pos迭代器失效cout *pos endl;auto it l.begin();while (it ! l.end()){it l.erase(it);}cout l.size() endl; }// 测试反向迭代器 void TestBiteList4() {int array[] {1, 2, 3, 4, 5};xyl::listint l(array, array sizeof(array) / sizeof(array[0]));auto rit l.rbegin();while (rit ! l.rend()){cout *rit ;rit;}cout endl;const xyl::listint cl(l);auto crit l.rbegin();while (crit ! l.rend()){cout *crit ;crit;}cout endl; } 【List 的迭代器】 迭代器有两种实现方式具体应根据容器底层数据结构实现 原生态指针比如vector。将原生态指针进行封装因迭代器使用形式与指针完全相同。 因此在自定义的类中必须实现以下方法 指针可以解引用迭代器的类中必须重载 operator*() 。指针可以通过 - 访问其所指空间成员迭代器类中必须重载 oprator-() 。指针可以 向后移动迭代器类中必须重载 operator() 与 operator(int) 。至于operator--() / operator--(int) 释放需要重载根据具体的结构来抉择双向链表可以向前移动所以需要重载如果是 forward_list 就不需要重载 -- 。迭代器需要进行是否相等的比较因此还需要重载 operator() 与 operator!() 。 2、list的反向迭代器  通过前面我们可以知道反向迭代器的 就是正向迭代器的 --反向迭代器的 -- 就是正向迭代器的 因此反向迭代器的实现可以借助正向迭代器即 反向迭代器内部可以包含一个正向迭代器对正向迭代器的接口进行包装即可。 templateclass Iterator class ReverseListIterator {// 注意这里typename的作用是明确告诉编译器Ref是Iterator类中的一个类型而不是静态成员变量// 否则编译器编译时就不知道Ref是Iterator中的类型还是静态成员变量// 因为静态成员变量也是按照 类名::静态成员变量名 的方式访问的 public:typedef typename Iterator::Ref Ref;typedef typename Iterator::Ptr Ptr;typedef ReverseListIteratorIterator Self; public:// 构造ReverseListIterator(Iterator it): _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; }; 三、list与vector的对比 vector 与 list 都是 STL 中非常重要的序列式容器由于两个容器的底层结构不同导致其特性以及应用场景不同其主要不同如下
http://wiki.neutronadmin.com/news/30877/

相关文章:

  • 云南建设人力资源网站南京网站建设服务
  • 网站黑色代码安徽省住房和建设执业资格注册中心网站
  • 网站建设方案主要有工作总结ppt模板免费下载
  • 在线做家装设计的网站保利威视 wordpress
  • 网站做好了怎样推广建站之星官网登陆
  • 网站运营实训报告总结郴州建设网站的公司
  • 菜鸟网站建设安徽网站建设优化推广
  • 洛阳网站设计建e室内设计网画图
  • 宜昌市做网站湖南做网站 安全还踏实磐石网络
  • 怎样做月嫂网站宁波自助建站系统
  • 保定网站建设报价济宁seo优化公司
  • ie的常用网站有哪些做平面设计好的网站
  • 博山网站seo做网站编辑前景
  • 帝国cms 网站地图 自定义可视化网站开发
  • 网站建设项目文档化妆品网站模板免费下载
  • 好的网站制作深圳网站制作开发排名
  • 网站开发的可行性郴州网络推广案例
  • 免费网站100m郑州优化公司有哪些
  • 免费模板网站建设网站制作的差异化定位
  • opencms 做的网站泊头市网站制作公司
  • 福州建设部官方网站网站建设课程ppt
  • 那些网站可以做淘宝店铺推广知名网站建设多少钱
  • 宁波企业制作网站自我介绍的网页设计作业
  • wordpress+手册主题搜索引擎优化的五个方面
  • 电影网站怎么做的wordpress除了首页都是404
  • 溧水城市建设招标网站2345网址导航浏览器下载
  • wordpress 修改评论邢台网站建设网络优化
  • 网站个人主页站外seo推广
  • 网站后台修改图片如何让百度分享按钮在网站每个页面都有
  • 怎么用优盘做网站登录密钥叮当app制作官网