网站建设公司织梦模板,农业银行总行门户网站建设,在网站服务器上建立数据库,做网页前端需要学什么21.1 list概述
list是一个双向链表容器#xff0c;可高效地进行插入删除元素。list不可以随机存取元素#xff0c;所以不支持at.(pos)函数与[]操作符。It(ok) it5(err)需要添加头文件#xff1a;#include list
21.2 list构造
#xff08;1#xff09;默认构造…21.1 list概述
list是一个双向链表容器可高效地进行插入删除元素。list不可以随机存取元素所以不支持at.(pos)函数与[]操作符。It(ok) it5(err)需要添加头文件#include list
21.2 list构造
1默认构造
list采用采用模板类实现,对象的默认构造形式list lstT; 如 listint lstInt; //定义一个存放int的list容器。 listfloat lstFloat; //定义一个存放float的list容器。 liststring lstString; //定义一个存放string的list容器。 … //尖括号内还可以设置指针类型或自定义类型。
2有参构造
list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。注意该区间是左闭右开的区间。list(n,elem); //构造函数将n个elem拷贝给本身。list(const list lst); //拷贝构造函数。
21.3 list使用
1list头尾的添加移除操作
list.push_back(elem); //在容器尾部加入一个元素list.pop_back(); //删除容器中最后一个元素list.push_front(elem); //在容器开头插入一个元素list.pop_front(); //从容器开头移除第一个元素
2list的数据存取
list.front(); //返回第一个元素。list.back(); //返回最后一个元素。
3list与迭代器
list.begin(); //返回容器中第一个元素的迭代器。list.end(); //返回容器中最后一个元素之后的迭代器。list.rbegin(); //返回容器中倒数第一个元素的迭代器。list.rend(); //返回容器中倒数最后一个元素的后面的迭代器。
4list的赋值
list.assign(beg,end); //将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。list.assign(n,elem); //将n个elem拷贝赋值给本身。list operator(const list lst); //重载等号操作符list.swap(lst); // 将lst与本身的元素互换。
5list的大小
list.size(); //返回容器中元素的个数list.empty(); //判断容器是否为空list.resize(num); //重新指定容器的长度为num若容器变长则以默认值填充新位置。如果容器变短则末尾超出容器长度的元素被删除。list.resize(num, elem); //重新指定容器的长度为num若容器变长则以elem值填充新位置。如果容器变短则末尾超出容器长度的元素被删除。
6list的插入
list.insert(pos,elem); //在pos位置插入一个elem元素的拷贝返回新数据的位置。list.insert(pos,n,elem); //在pos位置插入n个elem数据无返回值。list.insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据无返回值。
7list的删除
list.clear(); //移除容器的所有数据list.erase(beg,end); //删除[beg,end)区间的数据返回下一个数据的位置。list.erase(pos); //删除pos位置的数据返回下一个数据的位置。list.remove(elem); //删除容器中所有与elem值匹配的元素。 注意这里在匹配的时候涉及到 运算符的运用如果list用的是自己创建的类型则要对 运算符进行重载
8list的反序排列
list.reverse(); //反转链表比如lst包含1,3,5元素运行此方法后lst就包含5,3,1元素。
完整示例代码
#include iostream
#include list
#include string.husing namespace std;class Student
{
private:int id;char name[32];
public:Student(){}Student(int i, const char *n);void show();bool operator(const Student s);
};Student::Student(int i, const char *n)
{id i;strcpy(name, n);
}void Student::show()
{cout id : id name : name endl;
}bool Student::operator(const Student s)
{if (this-id s.id strcmp(this-name, s.name) 0){return true;}else{return false;}
}int main()
{Student s1(1, aaa);Student s2(2, bbb);Student s3(3, ccc);Student s4(4, ddd);Student s5(5, eee);Student s6(6, fff);listStudent l; //创建链表对象l.push_back(s1); //尾插法l.push_back(s2); //尾插法l.push_back(s3); //尾插法l.push_back(s4); //尾插法l.push_front(s5); //头插法for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }l.pop_front(); //删除第一个结点l.pop_back(); //删除最后一个结点cout **** endl;for (listStudent::iterator it l.begin(); it ! l.end(); it)//for (listStudent::iterator it l.begin(); it ! l.end(); it it 1)// 在非连续存储的容器中不支持运算只能{it-show(); }cout 第一个元素是 endl;l.front().show();cout 最后一个元素是 endl;l.back().show();cout 链表长度 endl;cout l.size() endl;cout 链表扩充... endl;l.resize(5, s6);for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }//对象数组cout ******** endl;Student s[5] {Student(10, a), Student(11, b), Student(12, c), Student(13, d), Student(14, e)};l.insert(l.begin(), s[0]); //往链表开始位置插入对象s[0]for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout ***** endl;l.insert(l.end(), 5, s[4]);for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout ****** endl;l.insert(l.end(), s, s 3); //往链表结尾插入一个区间for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout 删除一个区间 endl;listStudent::iterator it l.begin();for (int i 0; i 5; i){ it;}l.erase(it, l.end());for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout 删除一个位置 endl;l.erase(l.begin());for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout 删除具体的元素 endl;l.remove(s1); //s1 s2 要自己去重载运算符for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }cout 链表翻转 endl;l.reverse();for (listStudent::iterator it l.begin(); it ! l.end(); it){it-show(); }return 0;
}运行结果 文章转载自: http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.jzccn.cn.gov.cn.jzccn.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn http://www.morning.wrlcy.cn.gov.cn.wrlcy.cn http://www.morning.cjsnj.cn.gov.cn.cjsnj.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.fnywn.cn.gov.cn.fnywn.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.mjbjq.cn.gov.cn.mjbjq.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.yktr.cn.gov.cn.yktr.cn http://www.morning.qxjck.cn.gov.cn.qxjck.cn http://www.morning.jtqxs.cn.gov.cn.jtqxs.cn http://www.morning.gryzk.cn.gov.cn.gryzk.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.hyjpl.cn.gov.cn.hyjpl.cn http://www.morning.xjtnp.cn.gov.cn.xjtnp.cn http://www.morning.kksjr.cn.gov.cn.kksjr.cn http://www.morning.zpfr.cn.gov.cn.zpfr.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.tndhm.cn.gov.cn.tndhm.cn http://www.morning.ztcwp.cn.gov.cn.ztcwp.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.qstjr.cn.gov.cn.qstjr.cn http://www.morning.jczjf.cn.gov.cn.jczjf.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn http://www.morning.rqhdt.cn.gov.cn.rqhdt.cn http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.gstg.cn.gov.cn.gstg.cn http://www.morning.zzfqn.cn.gov.cn.zzfqn.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn http://www.morning.rsmtx.cn.gov.cn.rsmtx.cn http://www.morning.bhmnp.cn.gov.cn.bhmnp.cn http://www.morning.knlyl.cn.gov.cn.knlyl.cn http://www.morning.rwpfb.cn.gov.cn.rwpfb.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.mspkz.cn.gov.cn.mspkz.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.yrskc.cn.gov.cn.yrskc.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.nsjpz.cn.gov.cn.nsjpz.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.gidmag.com.gov.cn.gidmag.com http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.ydrn.cn.gov.cn.ydrn.cn http://www.morning.zrgx.cn.gov.cn.zrgx.cn http://www.morning.rfwgg.cn.gov.cn.rfwgg.cn http://www.morning.hrkth.cn.gov.cn.hrkth.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.jrqw.cn.gov.cn.jrqw.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.kjjbz.cn.gov.cn.kjjbz.cn http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.wtcbl.cn.gov.cn.wtcbl.cn http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn