卓商网站建设,怎么使用网站上的模板,在线设计平台市场分析,用jsp做网站的难点大家好我是沐曦希#x1f495; 文章目录一、前言二、构造三、迭代器四、增删查改1.头插头删2.尾插尾删3.查找和插入4.删除五、其他成员函数1.排序和去重2.splice和remove3.resize一、前言
list本质是带头双向循环链表#xff0c;本文只对list的一些常用接口进行说明#xf… 大家好我是沐曦希 文章目录一、前言二、构造三、迭代器四、增删查改1.头插头删2.尾插尾删3.查找和插入4.删除五、其他成员函数1.排序和去重2.splice和remove3.resize一、前言
list本质是带头双向循环链表本文只对list的一些常用接口进行说明对于其他一些接口可自行查看文档C Reference
二、构造 构造函数 (constructor)接口说明list (size_type n, const value_type val value_type())构造的list中包含n个值为val的元素list()构造空的listlist (const list x)拷贝构造函数list (InputIterator first, InputIterator last)用[first, last)区间中的元素构造list
void testlist1()
{listint lt1; // 无参构造listint lt2(5, 1); // n个val构造listint lt3(lt2.begin(), lt2.end()); // 迭代器区间构造listint lt4(lt3); // 拷贝构造
}三、迭代器 函数声明接口说明begin end返回第一个元素的迭代器返回最后一个元素下一个位置的迭代器rbegin rend返回第一个元素的reverse_iterator,即end位置返回最后一个元素下一个位置的reverse_iterator,即begin位置void testlist2()
{//正向迭代器listint lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);listint::iterator it lt1.begin();while (it ! lt1.end()){cout *it ;it;}cout endl;
}void testlist2()
{listint lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);listint::reverse_iterator rit lt1.rbegin();while (rit ! lt1.rend()){cout *rit ;rit;}cout endl;
}注意:
1. begin与end为正向迭代器对迭代器执行操作迭代器向后移动
2. rbegin(end)与rend(begin)为反向迭代器对迭代器执行操作迭代器向前移动
四、增删查改
1.头插头删 void testlist3()
{listint lt;lt.push_front(1);lt.push_front(2);lt.push_front(3);lt.push_front(4);for (const auto e : lt)cout e ;cout endl;lt.pop_front();lt.pop_front();for (const auto e : lt)cout e ;cout endl;
}2.尾插尾删 void testlist4()
{listint lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);for (const auto e : lt)cout e ;cout endl;lt.pop_back();lt.pop_back();for (const auto e : lt)cout e ;cout endl;
}3.查找和插入
在list容器中没有提供find函数可以通过算法库提供find进行查找
#includealgorithm
template class InputIterator, class T
InputIterator find (InputIterator first, InputIterator last, const T val);find和insert可以相互配合使用。 1.通过find找到位置插入 2.找到位置后插入n个val的值 3.找到位置后插入迭代器的区间 void testlist5()
{listint lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);auto pos find(lt.begin(), lt.end(), 3);// 1.在pos之前插入一个值if (pos ! lt.end())lt.insert(pos, 30); //insert以后pos没有失效for (const auto e : lt)cout e ;cout endl;cout *pos endl;// 2.插入n个数据pos find(lt.begin(), lt.end(), 3);if (pos ! lt.end())lt.insert(pos, 4, 10);for (const auto e : lt)cout e ;cout endl;// 3.插入一个迭代器区间vectorint v(5, 20);pos find(lt.begin(), lt.end(), 10);if (pos ! lt.end())lt.insert(pos, v.begin(), v.end());for (const auto e : lt)cout e ;cout endl;
}4.删除 void testlist6()
{listint lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);auto pos find(lt.begin(), lt.end(), 3);if (pos ! lt.end())lt.erase(pos);for (auto e : lt)cout e ;cout endl;pos find(lt.begin(), lt.end(), 4);if (pos ! lt.end())lt.erase(pos, lt.end());for (auto e : lt)cout e ;cout endl;
}注意对于list的insert的pos位置不会失效在这个地方只是在pos位置前增加节点改变链接pos位置并不会变成野指针。
五、其他成员函数
1.排序和去重
sort
算法库有一个sort函数但是list自己实现了因为算法库的sort不能排序list
算法库里的sort对于物理空间是连续的只有vector和string能够使用而对于list来说物理空间并不是连续的并不适用所以list自己提供了一个sort进行排序,此外链表的排序是归并排序。 void testlist7()
{listint lt;lt.push_back(12);lt.push_back(1);lt.push_back(6);lt.push_back(9);lt.push_back(4);lt.push_back(8);lt.push_back(10);for (auto e : lt)cout e ;cout endl;lt.sort();for (auto e : lt)cout e ;cout endl;
}unique
对于unique:用来删除链表中连续的重复元素但是注意一定是要先排完序在进行删除如果没有进行排序而直接进行去重的话会导致去重去不完全
void testlist8()
{listint lt;lt.push_back(12);lt.push_back(9);lt.push_back(1);lt.push_back(12);lt.push_back(12);lt.push_back(9);lt.push_back(8);lt.push_back(9);lt.push_back(12);lt.unique();for (auto e : lt)cout e ;cout endl;lt.sort();lt.unique();for (auto e : lt)cout e ;cout endl;
}2.splice和remove
splice void testlist9()
{//转移到某个位置listint lt1(5, 10);listint lt2(4, 7);lt1.splice(lt1.begin(), lt2);for (auto e : lt1)cout e ;cout endl;//从某个位置转移listint lt3(4, 10);listint lt4(4, 5);lt3.splice(lt3.begin(), lt4, lt4.begin());for (auto e : lt3)cout e ;cout endl;//迭代器区间转移listintlt5(3, 10);listintlt6(3, 20);lt5.splice(lt5.begin(), lt6, lt6.begin(), lt6.end());for (auto e : lt5)cout e ;cout endl;
}remove remove可以直接删除list中指定的数据
void testlist10()
{listint lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);lt.push_back(1);lt.push_back(1);lt.remove(3);for (auto e : lt)cout e ;cout endl;lt.remove(1);for (auto e : lt)cout e ;cout endl;
}3.resize list的resize很少用
void testlist11()
{listint lt(5, 10);lt.resize(3);for (auto e : lt)cout e ;cout endl;lt.resize(5);for (auto e : lt)cout e ;cout endl;lt.resize(7, 10);for (auto e : lt)cout e ;cout endl;
} 文章转载自: http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.qbpqw.cn.gov.cn.qbpqw.cn http://www.morning.yhljc.cn.gov.cn.yhljc.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.rgmd.cn.gov.cn.rgmd.cn http://www.morning.qsy36.cn.gov.cn.qsy36.cn http://www.morning.kgnnc.cn.gov.cn.kgnnc.cn http://www.morning.xgjhy.cn.gov.cn.xgjhy.cn http://www.morning.nhbhc.cn.gov.cn.nhbhc.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.lbbrw.cn.gov.cn.lbbrw.cn http://www.morning.rqwwm.cn.gov.cn.rqwwm.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.zyrp.cn.gov.cn.zyrp.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.gynls.cn.gov.cn.gynls.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.wcgfy.cn.gov.cn.wcgfy.cn http://www.morning.mlpch.cn.gov.cn.mlpch.cn http://www.morning.kxbdm.cn.gov.cn.kxbdm.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.nqpxs.cn.gov.cn.nqpxs.cn http://www.morning.qdlr.cn.gov.cn.qdlr.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.jsphr.cn.gov.cn.jsphr.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.fqyxb.cn.gov.cn.fqyxb.cn http://www.morning.bygyd.cn.gov.cn.bygyd.cn http://www.morning.rnrfs.cn.gov.cn.rnrfs.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.rckdq.cn.gov.cn.rckdq.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.xqgtd.cn.gov.cn.xqgtd.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.tlfzp.cn.gov.cn.tlfzp.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn http://www.morning.dgpxp.cn.gov.cn.dgpxp.cn http://www.morning.phnbd.cn.gov.cn.phnbd.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.ndynz.cn.gov.cn.ndynz.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.webife.com.gov.cn.webife.com http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.qbkw.cn.gov.cn.qbkw.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.linzhigongmao.cn.gov.cn.linzhigongmao.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn http://www.morning.zshuhd015.cn.gov.cn.zshuhd015.cn http://www.morning.zymgs.cn.gov.cn.zymgs.cn http://www.morning.gbsfs.com.gov.cn.gbsfs.com http://www.morning.nnqrb.cn.gov.cn.nnqrb.cn http://www.morning.drnfc.cn.gov.cn.drnfc.cn http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.nhzzn.cn.gov.cn.nhzzn.cn http://www.morning.rbffj.cn.gov.cn.rbffj.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.bfwk.cn.gov.cn.bfwk.cn http://www.morning.gygfx.cn.gov.cn.gygfx.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn