wp建站,wordpress 收录插件,英文外贸商城网站设计,几个有效网址谢谢list基本概念 功能#xff1a;将数据进行链式存储 链表#xff08;list#xff09;是一种物理存储单元上非连续的存储结构#xff0c;数据元素的逻辑顺序是通过链表中的指针链接实现的 链表的组成#xff1a;链表由一系列结点组成结点的组成#xff1a;一个是存储数据元素…list基本概念 功能将数据进行链式存储 链表list是一种物理存储单元上非连续的存储结构数据元素的逻辑顺序是通过链表中的指针链接实现的 链表的组成链表由一系列结点组成结点的组成一个是存储数据元素的数据域另一个是存储下一个结点地址的指针域STL中的链表是一个双向循环链表 由于链表的存储方式并不是连续的内存空间因此链表list中的迭代器只支持前移和后移属于双向迭代器
list的优点 采用动态存储分配不会造成内存浪费和溢出 链表执行插入和删除操作十分方便修改指针即可不需要移动大量元素 list的缺点 链表灵活但是空间(指针域) 和 时间遍历额外耗费较大 List有一个重要的性质插入操作和删除操作都不会造成原有list迭代器的失效这在vector是不成立的。
总结STL中List和vector是两个最常被使用的容器各有优缺点 1.list构造函数
// list构造函数
/*
listT lst; //list采用采用模板类实现,对象的默认构造形式
list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。
list(n,elem); //构造函数将n个elem拷贝给本身。
list(const list lst); //拷贝构造函数。
*/
#include bits/stdc.h
using namespace std;
void printlist(listint l)
{for (listint::iterator it l.begin(); it ! l.end(); it){cout *it ;}cout endl;
}
void test01()
{listint l1; // 默认构造// 添加数据l1.push_back(10);l1.push_back(20);l1.push_back(30);l1.push_back(40);printlist(l1); // 10 20 30 40// 区间方式构造listint l2(l1.begin(), l1.end());printlist(l2); // 10 20 30 40listint l3;l3 l2;printlist(l3); // 10 20 30 40listint l4(3, 100); // n个elementprintlist(l4); // 100 100 100
}
int main()
{test01();
}
/*
总结
list构造方式同其他几个STL常用容器熟练掌握即可
*/2.list 赋值和交换
// list 赋值和交换
/*
list operator(const list lst); //重载等号操作符
assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem); //将n个elem拷贝赋值给本身。
swap(lst); //将lst与本身的元素互换。
*/
#include bits/stdc.h
using namespace std;
void printlist(listint l)
{for (listint::iterator it l.begin(); it ! l.end(); it){cout *it ;}cout endl;
}
void test01()
{listint l1;// 添加数据l1.push_back(10);l1.push_back(20);l1.push_back(30);l1.push_back(40);printlist(l1); // 10 20 30 40listint l2;l2 l1; //赋值printlist(l2); // 10 20 30 40listint l3;l3.assign(l2.begin(), l2.end());printlist(l3); // 10 20 30 40listint l4;l4.assign(3, 100); // n个element赋值printlist(l4); // 100 100 100// 交换l1.swap(l4);printlist(l1);printlist(l4);
}
int main()
{test01();
}
/*
总结
list赋值和交换操作能够灵活运用即可
*/3.list 大小操作
// list 大小操作
/*
size(); //返回容器中元素的个数
empty(); //判断容器是否为空
resize(num); //重新指定容器的长度为num
若容器变长则以默认值填充新位置。
若容器变短则末尾超出容器长度的元素被删除。
resize(num, elem); //重新指定容器的长度为num
若容器变长则以elem值填充新位置。
若容器变短则末尾超出容器长度的元素被删除。
*/
#include bits/stdc.h
using namespace std;
void printlist(listint l)
{for (listint::iterator it l.begin(); it ! l.end(); it){cout *it ;}cout endl;
}
void test01()
{listint l1;// 添加数据l1.push_back(10);l1.push_back(20);l1.push_back(30);l1.push_back(40);printlist(l1); // 10 20 30 40if (l1.empty()){cout l1为空 endl;}else{cout l1不为空 endl;cout l1的元素个数为 l1.size() endl; // 4l1.resize(6);printlist(l1); // 10 20 30 40 0 0l1.resize(7, 555);printlist(l1); // 10 20 30 40 0 0 555}
}
int main()
{test01();
}
/*
总结
判断是否为空 — empty
返回元素个数 — size
重新指定个数 — resize
*/4.list 插入和删除
// list 插入和删除
/*
push_back(elem);//在容器尾部加入一个元素
pop_back();//删除容器中最后一个元素
push_front(elem);//在容器开头插入一个元素
pop_front();//从容器开头移除第一个元素
insert(pos,elem);//在pos位置插elem元素的拷贝返回新数据的位置。
insert(pos,n,elem);//在pos位置插入n个elem数据无返回值。
insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据无返回值。
clear();//移除容器的所有数据
erase(beg,end);//删除[beg,end)区间的数据返回下一个数据的位置。
erase(pos);//删除pos位置的数据返回下一个数据的位置。
remove(elem);//删除容器中所有与elem值匹配的元素。
*/
#include bits/stdc.h
using namespace std;
void printlist(listint l)
{for (listint::iterator it l.begin(); it ! l.end(); it){cout *it ;}cout endl;
}
void test01()
{listint l1;// 尾插l1.push_back(10);l1.push_back(20);l1.push_back(30);// 头插l1.push_front(100);l1.push_front(200);l1.push_front(300);printlist(l1); // 300 200 100 10 20 30// 尾删l1.pop_back(); // 300 200 100 10 20printlist(l1);// 头删l1.pop_front(); // 200 100 10 20printlist(l1);// insert插入listint::iterator it l1.begin(); // list不能随机访问it; // 不能itit2l1.insert(it, 1000); // 200 1000 100 10 20printlist(l1);it;l1.insert(it, 1001); // 200 1000 100 1001 10 20printlist(l1);// erase删除it l1.begin();l1.erase(it);printlist(l1); // 1000 100 1001 10 20// removel1.push_back(1000);l1.remove(1000); // 删除容器中所有与elem值匹配的元素。printlist(l1); // 100 1001 10 20// clear清空l1.clear();printlist(l1);
}
int main()
{test01();
}
/*
总结
尾插 — push_back
尾删 — pop_back
头插 — push_front
头删 — pop_front
插入 — insert
删除 — erase
移除 — remove
清空 — clear
*/5.list 数据存取
// list 数据存取
/*
front(); //返回第一个元素。
back(); //返回最后一个元素。
不能用[]和at因为list不用连续的空间存储不能随机访问
*/
#include bits/stdc.h
using namespace std;
void test01()
{listint l1;l1.push_back(10);l1.push_back(20);l1.push_back(30);l1.push_back(40);// 不能用l1[]和l1.at()// 因为list本质上是链表不是用连续线性空间存储数据迭代器也是不支持随机访问的// it1不行it可以it-1不行it--可以支持双向// 迭代器只能一个一个往后/前访问cout l1.front() l1.back() endl; // 10 40
}
int main()
{test01();
}
/*
总结
list容器中不可以通过[]或者at方式访问数据
返回第一个元素 — front
返回最后一个元素 — back
*/6.list 反转和排序
// list 反转和排序
/*
reverse(); //反转链表
sort(); //链表排序
*/
#include bits/stdc.h
using namespace std;
void printlist(listint l)
{for (listint::iterator it l.begin(); it ! l.end(); it){cout *it ;}cout endl;
}
bool cmp(int l1, int l2)
{ // 降序if (l1 l2)return true;return false;
}
void test01()
{listint l1;l1.push_back(20);l1.push_back(10);l1.push_back(50);l1.push_back(40);l1.push_back(30);printlist(l1); // 20 10 50 40 30// 反转reversel1.reverse();printlist(l1); // 30 40 50 10 20// sort(l1.begin(), l1.end());不对// 因为所有不支持随机访问迭代器的容器不能用标准算法其内部会提供相应算法l1.sort(); // 对 升序printlist(l1); // 10 20 30 40 50l1.sort(cmp); // 降序printlist(l1); // 50 40 30 20 10
}
int main()
{test01();
}
/*
总结
反转 — reverse
排序 — sort 成员函数
*/ 文章转载自: http://www.morning.nswcw.cn.gov.cn.nswcw.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn http://www.morning.fpngg.cn.gov.cn.fpngg.cn http://www.morning.kkhf.cn.gov.cn.kkhf.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.mumgou.com.gov.cn.mumgou.com http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.rlbfp.cn.gov.cn.rlbfp.cn http://www.morning.trjp.cn.gov.cn.trjp.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.ftmp.cn.gov.cn.ftmp.cn http://www.morning.pmbcr.cn.gov.cn.pmbcr.cn http://www.morning.mgkb.cn.gov.cn.mgkb.cn http://www.morning.gfqjf.cn.gov.cn.gfqjf.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.wdnkp.cn.gov.cn.wdnkp.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.rysmn.cn.gov.cn.rysmn.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.tpnch.cn.gov.cn.tpnch.cn http://www.morning.rgsnk.cn.gov.cn.rgsnk.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.jykzy.cn.gov.cn.jykzy.cn http://www.morning.swdnr.cn.gov.cn.swdnr.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.pntzg.cn.gov.cn.pntzg.cn http://www.morning.ggnkt.cn.gov.cn.ggnkt.cn http://www.morning.rccbt.cn.gov.cn.rccbt.cn http://www.morning.kbqws.cn.gov.cn.kbqws.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.c7629.cn.gov.cn.c7629.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn http://www.morning.nydgg.cn.gov.cn.nydgg.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn http://www.morning.yldgw.cn.gov.cn.yldgw.cn http://www.morning.jbpdk.cn.gov.cn.jbpdk.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.sbrrf.cn.gov.cn.sbrrf.cn http://www.morning.pcrzf.cn.gov.cn.pcrzf.cn http://www.morning.qqnjr.cn.gov.cn.qqnjr.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.btlmb.cn.gov.cn.btlmb.cn http://www.morning.tlrxp.cn.gov.cn.tlrxp.cn http://www.morning.prprj.cn.gov.cn.prprj.cn http://www.morning.lmxzw.cn.gov.cn.lmxzw.cn http://www.morning.cmdfh.cn.gov.cn.cmdfh.cn http://www.morning.hcqpc.cn.gov.cn.hcqpc.cn http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.hnrpk.cn.gov.cn.hnrpk.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.ryqsq.cn.gov.cn.ryqsq.cn http://www.morning.qlsyf.cn.gov.cn.qlsyf.cn http://www.morning.rgwrl.cn.gov.cn.rgwrl.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.btypn.cn.gov.cn.btypn.cn