list容器

176 阅读4分钟

List容器基本概念

功能:将数据进行链式存储

链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的

链表的组成:链表由一系列节点组成

节点的组成:一个是鵆数据元素的数据域,另一个是存储下一个结点地址的指针域

STL中的链表是一个双向循环链表,由于链表的存储方式并不是连续的内存空间,因此链表中list中的迭代器只支持迁移和后移,属于双向迭代器

list的优点:

  1. 采用动态存储分配,不会造成内存浪费和溢出
  2. 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
  3. List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这是在vector是不成立的。

list缺点:

  1. 链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大

总结:STL中List和vector是两个最常被使用的容器,各有优缺点!

List构造函数

函数原型:

  1. listlst; //list采用模板类实现,对象的默认构造形式
  2. list(beg ,end); //构造函数将[beg , end]区间中的元素拷贝给本身
  3. list(n , elem); //构造函数将n个elem拷贝给本身
  4. list(const list &lst); //拷贝构造函数

list构造函数案例:

void PrintList(list& L) {

for (list<int>::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(10, 100);
PrintList(L4);

}

List赋值和交换

函数原型:

  1. assign(beg , end); //将[beg , end]区间中的数据拷贝赋值给本身
  2. assign(n , elem); //将n个elem拷贝赋值给本身
  3. list& operator=(cosnt list &lst); //重载等号操作符
  4. swap(lst); //将lst与本身的元素互换

List赋值和交换案例:

void PrintList(const list& 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;	//operator= 赋值
PrintList(L2);

//区间拷贝赋值
list<int>L3;
L3.assign(L1.begin(), L1.end());
PrintList(L3);

//n个elem赋值
list<int>L4;
L4.assign(10, 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;
L2.assign(10, 100);

cout << "交换前: " << endl;
PrintList(L2);
PrintList(L1);

L1.swap(L2);	//list与本身元素互换
cout << "交换后:" << endl;
PrintList(L2);
PrintList(L1);

}

List大小操作

函数原型:

  1. size(); //返回容器中元素的个数
  2. empty(); //判断内容器是否为空
  3. resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置,如果容器变短,则末 尾超出容器长度的元素被删除
  4. resize(num , elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置,如果容器变短,则末尾超出容器长度的元素被删除

List大小案例:

void PrintList(const list& 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);

//判断容器是否为空
if (!L1.empty())
{
	cout << "L1为空!" << endl;
}
else
{
	cout << "L1不为空!" << endl;
	cout << "L1元素个数为: " << L1.size() << endl;
}

//重新指定大小
L1.resize(10 , 100);  //容器变长、超出的部分以elem填充新位置
PrintList(L1);


L1.resize(3);   //容器变短、超出部分元素被删除
PrintList(L1);

}

List容器插入和删除

函数原型:

  1. push_back(elem); //在容器尾部加入一个元素
  2. pop_back(); //删除容器最后一个元素
  3. push_front(elem); //在容器开头添加一个元素
  4. pop_front(); //容器开头移除第一个元素
  5. insert(pos , elem); //在pos位置插入elem元素的拷贝,返回新数据的位置
  6. insert(pos , n ,elem); //在pos位置插入n个elem数据,无返回值
  7. insert(pos , beg , end); //在pos位置插入[beg , end]区间的数据,无返回值
  8. clear(); //移除容器的所有数据
  9. erase(beg ,end); //删除[beg , end]区间的数据,返回下一个数据的位置
  10. erase(pos); //删除pos位置的数据,返回下一个数据的位置
  11. remove(elem); //删除容器中所有与elem值匹配的元素

List容器插入和是删除案例:

void PrintList(const list& 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_front(100);
L1.push_front(200);
L1.push_front(300);

//遍历    300 200 100 10 20 30
PrintList(L1);

//尾删
L1.pop_back();
//300 200 100 10 20
PrintList(L1);

//头删
L1.pop_front();
//200 100 10 20
PrintList(L1);

//insert插入
list<int>::iterator it = L1.begin();  //创建一个迭代器
L1.insert(++it, 1000);  //++it 位置偏移了
// 200 1000 100 10 20
PrintList(L1);

//删除
it = L1.begin();
L1.erase(++it);
//200 100 10 20
PrintList(L1);

//移除
L1.push_back(10000);
L1.push_back(10000);
L1.push_back(10000);
L1.push_back(10000);
PrintList(L1);

L1.remove(10000);  //删除容器中所有与elem值匹配的元素
PrintList(L1);

//清空
L1.clear();
PrintList(L1);

}

List数据存取

函数原型:

  1. front(); //返回第一个元素
  2. back(); //返回最后一个元素

List数据存取案例:

void PrintList(const list& 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[0]不可以用[]访问list容器中元素

//l.at(0) 不可以用at方式访问list容器中的元素

//原因是list本质链表,不是用连续性空间存储数据,迭代器也不支持随机访问的

cout << "第一个元素为:" << l.front() << endl;
cout << "最后一个元素:" << l.back() << endl;


//验证迭代器是不支持随机访问的
list<int>::iterator it = l.begin();
it++;  //支持双向
it--;
//it = it + 1;    不支持随机访问

}

List容器反转和排序

函数原型:

  1. reversr(); //反转链表
  2. sort(); //链表排序

List容器反转和排序案例:

void PrintList(const list& 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(70);
L.push_back(60);
L.push_back(40);
L.push_back(20);

cout << "反转前:" << endl;
PrintList(L);

//反转
L.reverse();
cout << "反转后:" << endl;
PrintList(L);

}

//仿函数

bool myCompare(int v1 , int v2) {

//降序  就让第一个数大于第二个数
return v1 > v2;

}

//排序 void test02() {

list<int>L;
L.push_back(10);
L.push_back(70);
L.push_back(60);
L.push_back(40);
L.push_back(20);

cout << "排序前前:" << endl;
PrintList(L);

//所有不支持随机访问迭代器的容器,不可以用标准算法
//不支持随机访问迭代器的容器,内部会提供对应一些算法
//sort(L.begin(), L.end()); 


//排序
L.sort();   //默认排序规则	从小到大 升序
cout << "反转后:" << endl;
PrintList(L);

L.sort(myCompare);		//降序
PrintList(L);

}