C++回顾(二十一)—— list容器

21.1 list概述

  • list是一个双向链表容器,可高效地进行插入删除元素。
  • list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符。It++(ok) it+5(err)
  • 需要添加头文件:#include <list>

21.2 list构造

(1)默认构造

list采用采用模板类实现,对象的默认构造形式:list lstT; 如:
list<int> lstInt; //定义一个存放int的list容器。
list<float> lstFloat; //定义一个存放float的list容器。
list<string> lstString; //定义一个存放string的list容器。

//尖括号内还可以设置指针类型或自定义类型。
在这里插入图片描述

(2)有参构造

  • list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。注意该区间是左闭右开的区间。
  • list(n,elem); //构造函数将n个elem拷贝给本身。
  • list(const list &lst); //拷贝构造函数。

21.3 list使用

(1)list头尾的添加移除操作

  • list.push_back(elem); //在容器尾部加入一个元素
  • list.pop_back(); //删除容器中最后一个元素
  • list.push_front(elem); //在容器开头插入一个元素
  • list.pop_front(); //从容器开头移除第一个元素
    在这里插入图片描述
    在这里插入图片描述

(2)list的数据存取

  • list.front(); //返回第一个元素。
  • list.back(); //返回最后一个元素。

(3)list与迭代器

  • list.begin(); //返回容器中第一个元素的迭代器。
  • list.end(); //返回容器中最后一个元素之后的迭代器。
  • list.rbegin(); //返回容器中倒数第一个元素的迭代器。
  • list.rend(); //返回容器中倒数最后一个元素的后面的迭代器。
    在这里插入图片描述

(4)list的赋值

  • list.assign(beg,end); //将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。
  • list.assign(n,elem); //将n个elem拷贝赋值给本身。
  • list& operator=(const list &lst); //重载等号操作符
  • list.swap(lst); // 将lst与本身的元素互换。

(5)list的大小

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

(6)list的插入

  • list.insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
  • list.insert(pos,n,elem); //在pos位置插入n个elem数据,无返回值。
  • list.insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值。

(7)list的删除

  • list.clear(); //移除容器的所有数据
  • list.erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。
  • list.erase(pos); //删除pos位置的数据,返回下一个数据的位置。
  • list.remove(elem); //删除容器中所有与elem值匹配的元素。
    注意:这里在匹配的时候,涉及到 == 运算符的运用,如果list用的是自己创建的类型,则要对 = = 运算符进行重载
    在这里插入图片描述

(8)list的反序排列

  • list.reverse(); //反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素。

完整示例代码:

#include <iostream>
#include <list>
#include <string.h>

using namespace std;

class Student
{
private:
	int id;
	char name[32];
public:
	Student(){}
	Student(int i, const char *n);
	void show();
	bool operator==(const Student &s);
};

Student::Student(int i, const char *n)
{
	id = i;
	strcpy(name, n);
}

void Student::show()
{
	cout << "id : " << id << "     name : " << name << endl;
}

bool Student::operator==(const Student &s)
{
	if (this->id == s.id && strcmp(this->name, s.name) == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	Student s1(1, "aaa");
	Student s2(2, "bbb");
	Student s3(3, "ccc");
	Student s4(4, "ddd");
	Student s5(5, "eee");
	Student s6(6, "fff");

	list<Student> l;      //创建链表对象

	l.push_back(s1);      //尾插法
	l.push_back(s2);      //尾插法
	l.push_back(s3);      //尾插法
	l.push_back(s4);      //尾插法
	l.push_front(s5);     //头插法

	for (list<Student>::iterator it = l.begin(); it != l.end(); it++)
	{
		it->show();	
	}

	l.pop_front();        //删除第一个结点
	l.pop_back();         //删除最后一个结点

	cout << "****" << endl;
	for (list<Student>::iterator it = l.begin(); it != l.end(); it++)
	//for (list<Student>::iterator it = l.begin(); it != l.end(); it = it + 1)// 在非连续存储的容器中,不支持+运算,只能++
	{
		it->show();	
	}

	cout << "第一个元素是:" << endl;
	l.front().show();

	cout << "最后一个元素是:" << endl;
	l.back().show();

	cout << "链表长度:" << endl;
	cout << l.size() << endl;

	cout << "链表扩充..." << endl;
	l.resize(5, s6);
	for (list<Student>::iterator it = l.begin(); it != l.end(); it++)
	{
		it->show();	
	}

	//对象数组
	cout << "********" << endl;
	Student s[5] = {Student(10, "a"), Student(11, "b"), Student(12, "c"), Student(13, "d"), Student(14, "e")};
	l.insert(l.begin(), s[0]);     //往链表开始位置插入对象s[0]
	for (list<Student>::iterator it = l.begin(); it != l.end(); it++)
	{
		it->show();	
	}

	cout << "*****" << endl;
	l.insert(l.end(), 5, s[4]);
	for (list<Student>::iterator it = l.begin(); it != l.end(); it++)
	{
		it->show();	
	}

	cout << "******" << endl;
	l.insert(l.end(), s, s + 3);   //往链表结尾插入一个区间
	for (list<Student>::iterator it = l.begin(); it != l.end(); it++)
	{
		it->show();	
	}

	cout << "删除一个区间" << endl;
	list<Student>::iterator it = l.begin();
	for (int i = 0; i < 5; i++)
	{	
		it++;
	}
	l.erase(it, l.end());
	for (list<Student>::iterator it = l.begin(); it != l.end(); it++)
	{
		it->show();	
	}

	cout << "删除一个位置" << endl;
	l.erase(l.begin());
	for (list<Student>::iterator it = l.begin(); it != l.end(); it++)
	{
		it->show();	
	}

	cout << "删除具体的元素" << endl;
	l.remove(s1);    //s1 == s2       要自己去重载==运算符
	for (list<Student>::iterator it = l.begin(); it != l.end(); it++)
	{
		it->show();	
	}

	cout << "链表翻转" << endl;
	l.reverse();
	for (list<Student>::iterator it = l.begin(); it != l.end(); it++)
	{
		it->show();	
	}

	return 0;
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值