国外的ui设计思想网站,烟台企业做网站,网站开发需要哪些东西,网业截屏怎么截目录 一、list的使用
二.list的模拟实现
三.总结 一、list的使用
list的底层是双向链表结构#xff0c;双向链表中每个元素存储在互不相关的独立节点中#xff0c;在节点中通过指针指向 其前一个元素和后一个元素。
常见的list的函数的使用 std::listint It {1,…目录 一、list的使用
二.list的模拟实现
三.总结 一、list的使用
list的底层是双向链表结构双向链表中每个元素存储在互不相关的独立节点中在节点中通过指针指向 其前一个元素和后一个元素。
常见的list的函数的使用 std::listint It {1, 2, 3, 4, 5};通过迭代器访问元素
std::listint::iterator it It.begin();
while (it ! It.end()) {std::cout *it std::endl;it;
}在链表尾部插入元素
It.push_back(6);在链表头部插入元素
It.push_front(0);删除元素
It.remove(3); // 删除值为3的元素
It.erase(it); // 删除迭代器指向的元素排序链表
It.sort();反转链表
It.reverse(); 但需要注意的是迭代器失效 在list进行插入和删除操作时不仅操作的元素所在的迭代器会失效所有指向链表的迭代器、指针和引用都会失效。因此在进行操作后需要重新获取有效的迭代器。vector的使用也要注意这个问题
二.list的模拟实现
list的迭代器和 string与 vector不太一样没有vector那么天生条件优越。需要单独实现一个类来封装迭代器。
指针可以解引用迭代器的vector类中必须重载operator*()
指针可以通过-访问其所指空间成员迭代器类中必须重载oprator-()
指针可以向后移动迭代器类中必须重载operator()与operator(int)
迭代器需要进行是否相等的比较因此还需要重载operator()与operator!()。 #pragma once
#includeiostream
#includeassert.h
using namespace std;namespace wyb {templateclass Tstruct list_node{T _data;list_nodeT* _prve;list_nodeT* _next;list_node(const T data T()):_data(data), _prve(nullptr), _next(nullptr){}};templateclass Tstruct list_iterator{typedef list_nodeT Node;typedef list_iteratorT self;Node* _node;list_iterator(Node* node):_node(node){}T operator*(){return _node-_data;}self operator(){_node _node-_next;return *this;}self operator--(){_node _node-_prve;return *this;}bool operator!(const self s) const{return _node ! s._node;}bool operator(const self s) const{return _node s._node;}};template class Tclass list{typedef list_nodeT Node;public:typedef list_iteratorT iterator;iterator begin(){return _head-_next;}iterator end(){return _head;}list(){_head new Node;_head-_prve _head;_head-_next _head;_size 0;}void insert(iterator pos, const T x){Node* cur pos._node;Node* prve cur-_prve;Node* newnode new Node(x);newnode-_next cur;cur-_prve newnode;prve-_next newnode;newnode-_prve prve;_size;}void push_back(const T x){insert(end(), x);}void front_back(const T x){insert(begin(), x);}void pop_back(){erase(--end());}void pop_front(){erase(begin());}void erase(iterator pos){assert(pos ! end());Node* prve pos-_node-_prve;Node* next pos-_node-_next;prve-_next next;next-_prve prve;free(pos);_size--;}private:Node* _head;size_t _size;};void test_list(){listint It;It.push_back(1);It.push_back(2);It.push_back(3);It.push_back(4);It.push_back(5);listint::iterator itIt.begin();while (it ! It.end()){cout *it ;it;}cout endl;}
} 1、任意位置插入删除时list可以随意插入删除但是vector任意位置的插入删除效率低需要挪动元素尤其是插入时有时候需要异地扩容就需要开辟新空间拷贝元素释放旧空间效率很低
2、访问元素时vector支持随机访问但是list不支持随机访问 3、迭代器的使用上vector可以使用原生指针但是list需要对原生指针进行封装 4、空间利用上vector使用的是一个连续的空间空间利用率高而list使用的是零碎的空间空间利用率低
三.总结
以上关于list的模拟实现有点不全我后期我会补充一些进来。又不懂的地方可以随时联系。
创作不易希望大佬点赞关注。
文章转载自: http://www.morning.kpcky.cn.gov.cn.kpcky.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.ghrhb.cn.gov.cn.ghrhb.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.xqjh.cn.gov.cn.xqjh.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.nkpml.cn.gov.cn.nkpml.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.fgrkc.cn.gov.cn.fgrkc.cn http://www.morning.qdlr.cn.gov.cn.qdlr.cn http://www.morning.pcgjj.cn.gov.cn.pcgjj.cn http://www.morning.jhkzl.cn.gov.cn.jhkzl.cn http://www.morning.tkxyx.cn.gov.cn.tkxyx.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.yrpd.cn.gov.cn.yrpd.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.qmzhy.cn.gov.cn.qmzhy.cn http://www.morning.wrqw.cn.gov.cn.wrqw.cn http://www.morning.zztkt.cn.gov.cn.zztkt.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.hkchp.cn.gov.cn.hkchp.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.bsbcp.cn.gov.cn.bsbcp.cn http://www.morning.kdpal.cn.gov.cn.kdpal.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.pplxd.cn.gov.cn.pplxd.cn http://www.morning.rtryr.cn.gov.cn.rtryr.cn http://www.morning.tnyanzou.com.gov.cn.tnyanzou.com http://www.morning.btpzn.cn.gov.cn.btpzn.cn http://www.morning.ttdbr.cn.gov.cn.ttdbr.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.mhybs.cn.gov.cn.mhybs.cn http://www.morning.prprz.cn.gov.cn.prprz.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.rfxw.cn.gov.cn.rfxw.cn http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn http://www.morning.sfnjr.cn.gov.cn.sfnjr.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.rkgyx.cn.gov.cn.rkgyx.cn http://www.morning.rpth.cn.gov.cn.rpth.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.rgtp.cn.gov.cn.rgtp.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.srnhk.cn.gov.cn.srnhk.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.zdmrf.cn.gov.cn.zdmrf.cn http://www.morning.rmqlf.cn.gov.cn.rmqlf.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.ckhyj.cn.gov.cn.ckhyj.cn http://www.morning.nynpf.cn.gov.cn.nynpf.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.i-bins.com.gov.cn.i-bins.com http://www.morning.dfndz.cn.gov.cn.dfndz.cn http://www.morning.bryyb.cn.gov.cn.bryyb.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.yrnll.cn.gov.cn.yrnll.cn