list基本概念:
功能:
将数据进行链式存储
链表(list):
是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
链表的组成:
链表有一系列结点组成
结点组成:
一个存储数据元素的数据域,另一个是存储下一个结点地址的指针域
优点:
采用动态存储分配,不会造成内存浪费和溢出
链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
缺点:
链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大
由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
List有一个重要的性质,插入和删除操作都不会造成原有了list迭代器的失效,这在vector容器是不成立的
list构造函数:
#include<iostream>
using namespace std;
#include<list>
void PrintList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
//创建list容器
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);
//拷贝构造
list<int>L3(L2);
PrintList(L3);
//n个elem
list<int>L4(5, 100);
PrintList(L4);
}
int main()
{
test01();
system("pause");
return 0;
}
list赋值和交换:
#include<iostream>
using namespace std;
#include<list>
void PrintList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//赋值
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
PrintList(L1);
list<int>L2;
L2 = L1;
PrintList(L2);
//list<int>L3(L2.begin(), L2.end());
//PrintList(L3);
list<int>L3;
L3.assign(L2.begin(),L2.end());
PrintList(L3);
list<int>L4;
L4.assign(2, 100);
PrintList(L4);
}
//交换
void test02()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
list<int>L2(5, 100);
cout << "交换前:" << endl;
PrintList(L1);
PrintList(L2);
L1.swap(L2);
cout << "交换后:" << endl;
PrintList(L1);
PrintList(L2);
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
list大小操作:
#include<iostream>
using namespace std;
#include<list>
void PrintList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//赋值
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
PrintList(L1);
list<int>L2;
L2 = L1;
PrintList(L2);
//list<int>L3(L2.begin(), L2.end());
//PrintList(L3);
list<int>L3;
L3.assign(L2.begin(),L2.end());
PrintList(L3);
list<int>L4;
L4.assign(2, 100);
PrintList(L4);
}
//交换
void test02()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
list<int>L2(5, 100);
cout << "交换前:" << endl;
PrintList(L1);
PrintList(L2);
L1.swap(L2);
cout << "交换后:" << endl;
PrintList(L1);
PrintList(L2);
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
list插入和删除:
#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>l;
//尾插
l.push_back(10);
l.push_back(20);
l.push_back(30);
//头插
l.push_front(100);
l.push_front(200);
l.push_front(300);
printList(l);
//尾删
l.pop_back();
printList(l);
//头删
l.pop_front();
printList(l);
//insert插入
list<int>::iterator it = l.begin();
l.insert(++it,1000);
printList(l);
//删除
it = l.begin();
l.erase(++it);
printList(l);
//移除
l.remove(100);
printList(l);
//清空
l.clear();
printList(l);
}
int main()
{
test01();
system("pause");
return 0;
}
数据存取:
list容器不支持at访问数据,不支持[ ]访问数据
list容器的迭代器是双向迭代器,不支持随机访问
#include<iostream>
using namespace std;
#include<list>
void test01()
{
list<int>L;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_back(40);
//cout << L.at(0) << endl;//错误 不支持at访问数据
//cout << L[0] << endl;//错误 不支持[]访问数据
cout << "第一个元素为:" << L.front() << endl;
cout << "最后一个元素为:" << L.back() << endl;
//list容器的迭代器是双向迭代器,不支持随机访问
list<int>::iterator it = L.begin();
//it = it + 1; //错误,不可以跳跃访问,即便是+1
it++;
cout << *it << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
反转和排序:
所有不支持随机访问迭代器的容器,不可以用标准算法
#include<iostream>
using namespace std;
#include<list>
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 test01()
{
list<int>L1;
for (int i = 0; i < 10; i++)
{
L1.push_back(i);
}
//反转前
cout << "反转前:";
printList(L1);
//反转后
L1.reverse();
cout << "反转后:";
printList(L1);
//所有不支持随机访问迭代器的容器,不可以用标准算法
//排序 从小到大 升序
cout << "排序升序后:";
L1.sort();
printList(L1);
//降序
cout << "降序:";
L1.sort(mycompare);
printList(L1);
}
int main()
{
test01();
system("pause");
return 0;
}