建设网站用英文怎么说,于都网站建设,中国建设传媒网,网络广告投放的主要方式有一、list介绍及使用
1. list文档介绍
#xff08;1#xff09;list是可以在常数范围内#xff0c;在任意位置进行插入、删除的序列式容器#xff0c;并且该容器可以前后双向迭代。
#xff08;2#xff09;list的底层是带头结点的双向循环链表#xff0c;其中每个元素…一、list介绍及使用
1. list文档介绍
1list是可以在常数范围内在任意位置进行插入、删除的序列式容器并且该容器可以前后双向迭代。
2list的底层是带头结点的双向循环链表其中每个元素存储在互不相关的独立节点中在节点中通过指针指向其前一个元素和后一个元素。
3list和forward_list非常相似。最主要的不同在于forward_list是单链表只能朝前迭代。
4与其他的序列式容器相比array、vector、dequelist通常在任意位置的插入、删除元素的执行效率更高。
5与其他序列式容器相比list和forward_list最大的缺陷是不支持任意位置的随机访问。
2. list常用接口介绍
2.1 list的构造 2.2 list iterator的使用 2.3 list capacity 2.4 list 元素访问 2.5 list 元素修改 3. list迭代器失效问题
3.1迭代器失效 迭代器失效即迭代器所指向的节点无效在list中即该节点被删除了。 因为list的底层结构为带头节点的双向循环链表因此在list中插入元素是不会导致list迭代器失效只有在删除时才会失效并且失效的只是指向被删除节点的迭代器其他迭代器不会受到影响。
3.2 list可能导致迭代器失效的操作 pop_back、pop_front、erase、resize、assign……
3.3解决方法 在所有可能导致迭代器失效的操作之后在使用迭代器前对迭代器进行重新赋值。
二、list与vector的区别★ 注意 空间利用率上只是绝大多数情况下vector比list高因为list每个节点多了两个指针域但并不绝对。
三、模拟实现list
1.实现接口
1.1私有成员
void CreateHead();//创建头节点
Node* _head;
1.2默认成员
//默认构造函数
list();
//n个value构造
list(size_t n, const T value T());
templateclass Iterator
//区间构造
list(Iterator first, Iterator last);
//拷贝构造
list(const listT L);
//赋值运算符重载
listT operator(listT L)//析构函数
~list();
1.3迭代器
注意 list迭代器不能使用原生态的指针因为list空间不连续不能对指针进行算数运算。所以需要自己模拟封装迭代器对应类
templateclass T, class Ref, class Ptr
struct ListIterator;
templateclass Iterator
struct ListReverseIterator;iterator begin();
iterator end();
const_iterator cbegin() const;
const_iterator cend() const;
reverse_iterator rbegin();
reverse_iterator rend();
const_reverse_iterator crbegin() const;
const_reverse_iterator crend() const;
1.4容量
size_t size() const ;
bool empty() ;
void resize(size_t newSize, const T value T()) ;
1.5元素访问
T front();
const T front() const;
T back();
const T back() const ;
1.6元素修改
void push_front(const T value);
void pop_front();
void push_back(const T value) ;
void pop_back();
iterator insert(iterator it, const T value);
iterator erase(iterator it);
void clear();
void swap(listT L) ;
2.代码实现
#include iostream
using namespace std;namespace MyList {//链表节点templateclass Tstruct ListNode {ListNodeT* _next;ListNodeT* _prev;T _val;ListNode(const T value T()): _next(nullptr), _prev(nullptr), _val(value){}};//注意list迭代器不能使用原生态的指针//因为list空间不连续不能对指针进行算数运算//模拟封装迭代器类templateclass T, class Ref, class Ptrstruct ListIterator {typedef ListNodeT Node;typedef Ref ItRef;typedef Ptr ItPtr;typedef ListIteratorT, Ref, Ptr Self;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 s) const {return _node ! s._node;}bool operator(const Self s) const {return _node s._node;}public:Node* _node;};//模拟封装反向迭代器内部封装正向迭代器即可templateclass Iteratorstruct ListReverseIterator {//因为静态成员变量也可以通过类名::静态成员名称的方式进行访问//直接写会编译报错编译器不确定ItRef、ItPtr是静态成员变量还是类型//所以需要显示告诉编译器是typenametypedef typename Iterator::ItRef Ref;typedef typename Iterator::ItPtr Ptr;typedef ListReverseIteratorIterator Self;public:ListReverseIterator(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 rit) const {return _it ! rit._it;}bool operator(const Self rit) const {return _it rit._it;}public:Iterator _it;//正向迭代器};/**/templateclass Tclass list {typedef ListNodeT Node;public:typedef ListIteratorT, T, T* iterator;typedef ListIteratorT, const T, const T* const_iterator;typedef ListReverseIteratoriterator reverse_iterator;typedef ListReverseIteratorconst_iterator const_reverse_iterator;public:/*----------默认成员----------*///默认构造函数list() {CreateHead();}//n个value构造list(int n, const T value T()) {CreateHead();for (int i 0; i n; i) { push_back(value); }}templateclass Iterator//区间构造list(Iterator first, Iterator last) {CreateHead();while (first ! last) {push_back(*first);first;}}//拷贝构造list(const listT L) {CreateHead();auto it L.cbegin();while (it ! L.cend()) {push_back(*it);it;}}//赋值运算符重载listT operator(listT L) {this-swap(L);return *this;}//析构函数~list() {clear();delete _head;_head nullptr;}/*----------迭代器----------*/iterator begin() {return iterator(_head-_next);}iterator end() {return iterator(_head);}const_iterator cbegin() const {return const_iterator(_head-_next);}const_iterator cend() const {return const_iterator(_head);}reverse_iterator rbegin() {return reverse_iterator(end());}reverse_iterator rend() {return reverse_iterator(begin());}const_reverse_iterator crbegin() const {return const_reverse_iterator(cend());}const_reverse_iterator crend() const {return const_reverse_iterator(cbegin());}/*----------容量----------*/size_t size() const {Node* cur _head-_next;size_t count 0;while (cur ! _head) {count;cur cur-_next;}return count;}bool empty() {return _head-_next _head;}void resize(size_t newSize, const T value T()) {size_t oldSize size();if (newSize oldSize) {for (size_t i oldSize; i newSize; --i) { pop_back(); }}else {for (size_t i oldSize; i newSize; i) { push_back(value); }}}/*----------元素访问----------*/T front() {return *begin();}const T front() const {return *cbegin();}T back() {return *(--end);}const T back() const {return *(--cend);}/*----------元素修改----------*/void push_front(const T value) {insert(begin(), value);}void pop_front() {erase(begin());}void push_back(const T value) {insert(end(), value);}void pop_back() {erase(end()--);}iterator insert(iterator it, const T value) {Node* pos it._node;//插入方式将新节点连接进去再断开原链表Node* temp new Node(value);//(1)连接temp-_next pos;temp-_prev pos-_prev;//(2)断开temp-_prev-_next temp;temp-_next-_prev temp;return iterator(temp);}iterator erase(iterator it) {if (it end()) return end();Node* pos it._node;Node* ret pos-_next;pos-_prev-_next pos-_next;pos-_next-_prev pos-_prev;delete pos;return iterator(ret);}void clear() {auto it begin();while (it ! end()) {it erase(it);//记得重新赋值防止迭代器失效}}void swap(listT L) {std::swap(_head, L._head);}/*----------私有成员----------*/private:void CreateHead() {//创建头节点_head new Node();_head-_next _head;_head-_prev _head;}Node* _head;};
} 文章转载自: http://www.morning.rhfbl.cn.gov.cn.rhfbl.cn http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.rchsr.cn.gov.cn.rchsr.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.xhpnp.cn.gov.cn.xhpnp.cn http://www.morning.lpskm.cn.gov.cn.lpskm.cn http://www.morning.qrsm.cn.gov.cn.qrsm.cn http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn http://www.morning.bhrbr.cn.gov.cn.bhrbr.cn http://www.morning.pwxkn.cn.gov.cn.pwxkn.cn http://www.morning.wpkr.cn.gov.cn.wpkr.cn http://www.morning.nqmkr.cn.gov.cn.nqmkr.cn http://www.morning.cdrzw.cn.gov.cn.cdrzw.cn http://www.morning.dfltx.cn.gov.cn.dfltx.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.sbrpz.cn.gov.cn.sbrpz.cn http://www.morning.knlgk.cn.gov.cn.knlgk.cn http://www.morning.dzqyn.cn.gov.cn.dzqyn.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn http://www.morning.ptwzy.cn.gov.cn.ptwzy.cn http://www.morning.hlxpz.cn.gov.cn.hlxpz.cn http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.nlkm.cn.gov.cn.nlkm.cn http://www.morning.myxps.cn.gov.cn.myxps.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.fpjxs.cn.gov.cn.fpjxs.cn http://www.morning.cpfbg.cn.gov.cn.cpfbg.cn http://www.morning.jgttx.cn.gov.cn.jgttx.cn http://www.morning.tkhyk.cn.gov.cn.tkhyk.cn http://www.morning.zfcfx.cn.gov.cn.zfcfx.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.wyzby.cn.gov.cn.wyzby.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.rnzjc.cn.gov.cn.rnzjc.cn http://www.morning.yrms.cn.gov.cn.yrms.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn http://www.morning.bhrbr.cn.gov.cn.bhrbr.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.zzjpy.cn.gov.cn.zzjpy.cn http://www.morning.pqwjh.cn.gov.cn.pqwjh.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.rcrfz.cn.gov.cn.rcrfz.cn http://www.morning.kysport1102.cn.gov.cn.kysport1102.cn http://www.morning.msgcj.cn.gov.cn.msgcj.cn http://www.morning.glnxd.cn.gov.cn.glnxd.cn http://www.morning.zqdzg.cn.gov.cn.zqdzg.cn http://www.morning.gbwfx.cn.gov.cn.gbwfx.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.cpfbg.cn.gov.cn.cpfbg.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.rxlck.cn.gov.cn.rxlck.cn http://www.morning.zcmpk.cn.gov.cn.zcmpk.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn http://www.morning.bsxws.cn.gov.cn.bsxws.cn http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.mttck.cn.gov.cn.mttck.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.dhxnr.cn.gov.cn.dhxnr.cn http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.wyzby.cn.gov.cn.wyzby.cn