概念
list概念:将数据进行链式存储
list链表:物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表的指针链接实现的。
存储数据元素的数据域和存储下一个节点位置的指针域
由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
构造函数
list<T> lst;// 创建一个空的list
list(beg,end);// 将[beg,end)的元素拷贝给自己
list(n,elem);// 将n个elem拷贝给自己
list(const list &list);// 拷贝构造函数
- #include <iostream>
- #include <vector>
- #include <list>
- using namespace std;
- void printList(const list<int> &L){
- for(list<int>::const_iterator it=L.begin();it!=L.end();it++){
- cout<<*it<<" ";// *it表示迭代器it指向的元素,解引用
- }
- cout<<endl;
- }
- void test(){
- list<int> L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
- printList(L1);//`
- list<int> L2(L1.begin(),L1.end());
- printList(L2);// 10 20 30 40
- list<int> L3(L2);// 拷贝构造
- printList(L3);// 10 20 30 40
- list<int> L4(10,100);
- printList(L4);// 100 100 100 100 100 100 100 100 100 100
- }
- int main()
- {
- test();
- system("pause");
- return 0;
- }
复制代码 list复制和交换
assign(beg,end);//将[beg,end)区间内的元素拷贝到调用该方法的list中。
assign(n,elem);//将n个elem拷贝到调用该方法的list中。
list& operator=(const list &lst);//将lst拷贝到调用该方法的list中。
swap(list);//将调用该方法的list与lst交换。
- #include <iostream>
- #include <vector>
- #include <list>
- using namespace std;
- void printList(const list<int> &L)
- {
- for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
- void test1(){
- list<int> L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
- list<int> L2(L1);
- // L2=L1;
- printList(L2);
- list<int> L3;
- L3.assign(L1.begin(),L1.end());
- printList(L3);
- list<int> L4;
- L4.assign(5,100);
- printList(L4);
- }
- void test2(){
- list<int> L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
- list<int> L2;
- L2.assign(5,200);
- cout<<"交换前:"<<endl;
- printList(L1);
- printList(L2);
- L1.swap(L2);
- cout<<"交换后:"<<endl;
- printList(L1);
- printList(L2);
- }
- int main()
- {
- // test1();
- test2();
- system("pause");
- return 0;
- }
复制代码 list巨细操作
size();//返回list的巨细
empty();//判定list是否为空
resize(num);//调整list的巨细,若小于num,则用0填充,若大于num,则删除多余的元素
resize(num,elem);//调整list的巨细,若小于num,则用elem填充,若大于num,则删除多余的元素
clear();//清空list
- #include <iostream>
- #include <vector>
- #include<list>
- using namespace std;
- void printList(const list<int>& L){
- for(list<int>::const_iterator it=L.begin();it!=L.end();it++){
- cout<<*it<<" ";
- }
- cout<<endl;
- }
- void test(){
- list<int> L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
- if(L1.empty()){
- cout<<"L1为空"<<endl;
- }
- else{
- cout<<"L1不为空"<<endl;
- cout<<"L1的大小为:"<<L1.size()<<endl;
- }
- L1.resize(8);
- cout<<"调整L1的大小为10后:"<<endl;
- printList(L1);//10 20 30 40 0 0 0 0
- L1.resize(10,100);
- cout<<"调整L1的大小为5,填充100后:"<<endl;
- printList(L1);//10 20 30 40 0 0 0 0 100 100
- }
- int main()
- {
- test();
- system("pause");
- return 0;
- }
复制代码 list插入和删除
push_back(elem);//在链表尾部插入一个元素
pop_back();//在链表尾部删除一个元素
push_front(elem);//在链表头部插入一个元素
pop_front();//在链表头部删除一个元素
insert(pos,elem);//在pos位置插入一个值为elem的节点
insert(pos,n,elem);//在pos位置插入n个值为elem的节点
clear();//清空链表
erase(beg,end);//删除[beg,end)区间的节点
erase(pos);//删除pos位置的节点
remove(elem);//删除链表中所有值为elem的节点
- #include <iostream>
- #include <vector>
- #include<list>
- using namespace std;
- void printList(const list<int>& L){
- for(list<int>::const_iterator it=L.begin();it!=L.end();it++){
- cout<<*it<<" ";
- }
- cout<<endl;
- }
- void test(){
- list<int> L;
- //尾插
- L.push_back(10);
- L.push_back(20);
- L.push_back(30);
- //尾删
- L.pop_back();
- printList(L);//10 20
- //头插
- L.push_front(100);
- L.push_front(200);
- L.push_front(300);
- //头删
- L.pop_front();
- printList(L);//200 100 10 20
- //插入
- list<int>::iterator it=L.begin();
- L.insert(it,1000);
- printList(L);//1000 200 100 10 20
- //删除
- it=L.begin();
- L.erase(it);//删除it位置的元素
- printList(L);//200 100 10 20
- //移除
- L.push_back(10000);
- L.push_back(10000);
- L.push_back(10000);
- L.remove(10000);
- printList(L);//200 100 10 20
- //清空
- L.clear();
- printList(L);
- }
- int main()
- {
- test();
- system("pause");
- return 0;
- }
复制代码 list数据存取
- #include <iostream>
- #include <vector>
- #include<list>
- using namespace std;
- void test(){
- list<int> L;
- L.push_back(100);
- L.push_back(200);
- L.push_back(300);
- L.push_back(400);
- cout<<L.front()<<endl;//100
- cout<<L.back()<<endl;//400
- // list容器的迭代器是双向迭代器,不支持随机访问
- list<int>::iterator it=L.begin();// 迭代器it指向第一个元素
- cout<<*it<<endl;//100
- }
- int main()
- {
- test();
- system("pause");
- return 0;
- }
复制代码 list反转和排序
reverse();// 反转
sort();// 排序
- #include <iostream>
- #include <vector>
- #include<list>
- using namespace std;
- void printList(const list<int>& L){
- for(list<int>::const_iterator it=L.begin();it!=L.end();it++){
- cout<<*it<<" ";
- }
- cout<<endl;
- }
- bool myCompare(int v1,int v2){
- return v1>v2;
- }
- void test(){
- list<int> L1;
- L1.push_back(50);
- L1.push_back(30);
- L1.push_back(90);
- L1.push_back(40);
- printList(L1);// 50 30 90 40
- //反转
- L1.reverse();
- printList(L1);// 40 90 30 50
- //排序
- L1.sort();// 默认从小到大
- printList(L1);// 30 40 50 90
- L1.sort(myCompare);//大到小
- printList(L1);// 90 50 40 30
- }
- int main()
- {
- test();
- system("pause");
- return 0;
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |