兴安盟网站建设,网站备案复查 手机号码打不通,智慧团建系统登录,宝塔wordpress ssl证书系列文章目录 文章目录 系列文章目录前言list_nodeT#xff08;节点#xff09;_list_iteratorT, Ref, Ptr#xff08;迭代器#xff09;成员变量构造函数运算符重载 ListT#xff08;链表#xff09;成员变量构造函数析构函数区间构造函数拷贝构…系列文章目录 文章目录 系列文章目录前言list_nodeT节点_list_iteratorT, Ref, Ptr迭代器成员变量构造函数运算符重载 ListT链表成员变量构造函数析构函数区间构造函数拷贝构造赋值重载Modifiers修改器list的迭代器失效 前言
模拟实现list类 STL3.0(SGI版本)
list_node节点
//节点类
templateclass T
struct list_node
{//成员变量list_nodeT* _next;list_nodeT* _prev;T _data;//构造函数list_node(cosnt T x T()):_next(nullptr), _prev(nullptr),_data(x){}
};_list_iteratorT, Ref, Ptr迭代器
成员变量 templateclass T, class Ref, class Ptrstruct _list_iterator{//用类来封装node*typedef list_nodeT node;typedef _list__iteratorT, Ref, Ptr self;node* _node;};构造函数
//构造函数
_list_iterator(node* n):_node(n)
{}运算符重载
//Iterator
Ref operator*()
{return _node-_data;
}Ptr operator-()
{//it-_a1 it--_a1;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;
}List链表
成员变量
templateclass T
class list
{typedef list_nodeT node;public:typedef _list_iteratorT, T, T* iterator;typedef _list_iteratorT, const T, const T*const_iterator;private:node* _head;//节点指针
};构造函数
void empty_init()
{//创建头节点_head new node;_head-_next _head;_head-_prev _head;
}list()
{empty_init();
}析构函数
//析构函数
~list()
{ clear();//释放头节点delete _head;_head nullptr;
}区间构造函数
template class Iterator
list(Iterator first, Iterator last)
{empty_init();while (first ! last){push_back(*first);first;}
}拷贝构造
void swap(listT lt)
{std::swap(_head, lt._head);
}list(const listT lt)
{empty_init();listT tmp(lt.begin(), lt.end());swap(tmp);
}赋值重载
listT operator(listT tmp)
{swap(tmp);return *this;
}Modifiers修改器
void push_back(cosnt T x)
{insert(end(), x);
}void push_front(const T x)
{insert(begin(), x);
}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()){erase(it);}
}void pop_back()
{erase(--end);
}
void pop_front()
{erase(begin());
}list的迭代器失效
void TestListIterator1()
{int array[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };listint l(array, array sizeof(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, array sizeof(array) / sizeof(array[0]));auto it l.begin();while (it ! l.end()){l.erase(it); // it l.erase(it);}
}
文章转载自: http://www.morning.ydnx.cn.gov.cn.ydnx.cn http://www.morning.blznh.cn.gov.cn.blznh.cn http://www.morning.xwbwm.cn.gov.cn.xwbwm.cn http://www.morning.gsjw.cn.gov.cn.gsjw.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.hjjkz.cn.gov.cn.hjjkz.cn http://www.morning.bpmtr.cn.gov.cn.bpmtr.cn http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.zmyzt.cn.gov.cn.zmyzt.cn http://www.morning.rdmz.cn.gov.cn.rdmz.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.nlglm.cn.gov.cn.nlglm.cn http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.tkzqw.cn.gov.cn.tkzqw.cn http://www.morning.rtzd.cn.gov.cn.rtzd.cn http://www.morning.kyflr.cn.gov.cn.kyflr.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.pprxs.cn.gov.cn.pprxs.cn http://www.morning.rfqk.cn.gov.cn.rfqk.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.zwxfj.cn.gov.cn.zwxfj.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.hnhgb.cn.gov.cn.hnhgb.cn http://www.morning.mswkd.cn.gov.cn.mswkd.cn http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.xlmgq.cn.gov.cn.xlmgq.cn http://www.morning.ptdzm.cn.gov.cn.ptdzm.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.glnmm.cn.gov.cn.glnmm.cn http://www.morning.xkwyk.cn.gov.cn.xkwyk.cn http://www.morning.glnxd.cn.gov.cn.glnxd.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.sfnjr.cn.gov.cn.sfnjr.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn http://www.morning.wbxr.cn.gov.cn.wbxr.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.yhpl.cn.gov.cn.yhpl.cn http://www.morning.ffrys.cn.gov.cn.ffrys.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.vvbsxm.cn.gov.cn.vvbsxm.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.xkwyk.cn.gov.cn.xkwyk.cn http://www.morning.4r5w91.cn.gov.cn.4r5w91.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.hybmz.cn.gov.cn.hybmz.cn http://www.morning.qysnd.cn.gov.cn.qysnd.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn http://www.morning.xknmn.cn.gov.cn.xknmn.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn http://www.morning.ltpzr.cn.gov.cn.ltpzr.cn http://www.morning.rfmzc.cn.gov.cn.rfmzc.cn http://www.morning.qxgmp.cn.gov.cn.qxgmp.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.zgdnz.cn.gov.cn.zgdnz.cn http://www.morning.hjjkz.cn.gov.cn.hjjkz.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn