C++经典问题_23 STL(七) list容器

一. list的基本概念

① 功能:
  1. 将数据进行链式存储
  2. 链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
② 链表的组成
  1. 链表由一系列的节点组成
  2. 节点的组成: 一个是存储数据元素的数据域,另一个是存储下一个节点地址的指针域
③ STL的链表是一个双向循环链表


注意:

链表的存储方式并不是连续的内存空间
链表的list的迭代器只支持前移和后移,属于双向迭代器.
链表并不是连续的存储空间,所以不支持跨越式访问,只能前移和后移一位,不能使用(v.begin()+2)这种跨越式访问操作

④ list的优点
  1. 采用动态存储分配,不会造成内存浪费和溢出
  2. 链表执行插入和删除操作非常方便,效率较高,修改指针即可,不需要移动大量元素.
  3. 插入操作和删除操作都不会造成原有的list的迭代器的实效,这才vector是不成立的
⑤ list的缺点

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

二. list的构造函数

原型:

list<T> L; list采用模板类实现,对象的默认构造形式;
list<begin,end); 构造函数将[begin,end]区间中的元素拷贝给本身
list(n,elem); 构造函数将n个elem拷贝给本身
list(const list& lst); 拷贝构造函数

/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include <list>

void print_list(list<int> &L)
{
	for (list<int>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test_01(void)
{
	// 创建list容器
	list<int> L1;
	// 添加数据
	L1.push_back(10);
	L1.push_front(20);
	L1.push_back(30);
	L1.push_back(40);

	// 遍历容器打印
	print_list(L1);

	// 区间方式构造,其中begin()可以进行++和--操作,但是不能进行begin()+-或者en()+-操作
	list<int> L2(++L1.begin(), --L1.end());
	print_list(L2);

	// 拷贝构造
	list<int> L3(L2);
	print_list(L3);

	// N个elem
	list<int> L4(10, 1000);
	print_list(L4);

}
int main()
{
	test_01();
	system("pause");
	return 0;
}

三. list的赋值和交换

原型:

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

/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<list>

void print_list(const list<int> &L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test_01(void)
{
	list<int> L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_front(30);
	L1.push_front(40);
	print_list(L1);

	// 赋值操作符
	list<int> L2;
	L2 = L1; // operator= 赋值
	print_list(L2);

	// assign赋值
	list<int> L3;
	L3.assign(L2.begin(), L2.end());
	print_list(L3);

	list<int> L4;
	L4.assign(10, 100);
	print_list(L4);

	list<int> L5;
	L5.assign(++L2.begin(), --L2.end());
	print_list(L5);

	// 进行交换
	L4.swap(L5);
	cout << "交换后: " << endl;
	print_list(L4);
	print_list(L5);
}

int main()
{
	test_01();
	system("pause");
	return 0;
}

四. list大小操作

原型:

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

/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include <list>

void print_list(const list<int> &L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test_01(void)
{
	list<int> L;
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	L.push_back(40);
	print_list(L);

	// 判断容器是否为空
	if (L.empty())
	{
		cout << "L为空!" << endl;
	}
	else
	{
		cout << "L不为空!,L的大小为: " << L.size() << endl;
		// 然后是改变大小
		L.resize(10);// 多出来的部分填充为0
		print_list(L);

		// 如果是变小,就截取掉
		L.resize(1);
		print_list(L);

		// 用固定元素去填充
		L.resize(10, 10);
		print_list(L);
	}
}

int main()
{

	test_01();
	system("pause");
	return 0;
}

五. list的插入和删除

函数原型:

push_back(elem); 在容器的尾部插入一个元素
pop_back(); 删除容器中最后一个元素
push_front(elem)在容器的头部插入一个元素
pop_front()从容器的头部移除第一个元素
insert(pos,elem); 在pos位置插入elem元素的拷贝,返回新数据的位置,pos是迭代器的位置,不是索引
insert(pos,n,elem); 在pos位置插入n个elem数据,无返回值,pos是迭代器的位置,不是索引
insert(pos,begin,end); 在pos位置插入[begin,end]区间的数据,无返回值.pos是迭代器的位置,不是索引
clear(); 移除容器的所有数据
erase(begin,end) 删除[begin,end]区间的数据,返回下一个数据的位置
erase(pos); 删除pos位置的数据,返回下一个数据的位置
remove(elem); 删除容器中所有与elem值匹配的元素

/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<list>


void print_list(const list<int> &L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
int main()
{
	list<int> L;
	L.push_back(10);
	L.push_back(20);
	L.push_front(30);
	L.push_front(40);
	print_list(L);// 40,30,10,20

	// 头删除
	L.pop_front();
	print_list(L); // 30 10 20

	// 尾删
	L.pop_back();
	print_list(L); // 30 10

	// insert插入
	list<int>::iterator it = L.begin();
	L.insert(++it, 1000); // 30 1000 10
	print_list(L);

	// 删除,删除指定位置的数据
	it = L.begin();
	L.erase(++it); 

	// 移除,清空所有的数据
	L.erase(L.begin(), L.end());
	print_list(L);

	// remove移除固定值的元素
	L.push_back(1000);
	L.push_back(1000);
	L.push_back(1000);
	L.push_back(1);
	print_list(L); // 1000,1000,1000,1

	// 移除掉值为1000的元素
	L.remove(1000);
	print_list(L);// 1

	// 清空list
	L.clear();
	print_list(L);

	system("pause");
	return 0;
}

六. list的数据存取

  1. 对list中的数据进行存取
  2. front(); 返回第一个元素
  3. back(); 返回最后一个元素
  4. list不支持at和[]这种方式访问,它的内存空间不是连续的,不支持随机访问
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<list>


void test_01(void)
{
	list<int> L;
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	L.push_back(40);

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

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

	// 迭代器不支持随机访问
	list<int>::iterator it = L.begin();
	it++; // 可以加加
	//it = it + 1; 不支持+-运算符,但是可以支持递增递减操作符
	//不支持迭代器的随机访问
}
int main()
{
	test_01();

	system("pause");
	return 0;
}

七. list的反转和排序

原型:

reverse(); 反转链表
sort(); 排序

/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<list>

void print_list(const list<int> &L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void reverse_test(void)
{
	// 反转测试
	list<int> L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	print_list(L1); // 10 20 30 40

	// 反转
	L1.reverse();
	print_list(L1); // 40 30 20 10

	// 进行排序
	L1.sort(); // 10 20 30 40
	print_list(L1);
	// 算法里面的sort()不能使用
	// sort(L1.begin(),L1.end()) 运行时报错,因为L1不支持随机访问,所以不能是用算法里面的sort,只能用自身的sort函数

}
int main()
{
	reverse_test();
	system("pause");
	return 0;
}

排序案例:

  1. 将Person自定义数据类型进行排序,Person中属性有姓名,年龄,身高
  2. 按照年龄进行升序,如果年龄相同按照身高进行降序
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include <list>
#include<string>
class Person
{
public:
	Person(string name, int age, int height)
	{
		this->mName = name;
		this->mAge = age;
		this->mHeight = height;
	}
	void show_person()
	{
		cout << "姓名: " << this->mName << " 年龄: " << this->mAge << " 身高: " << this->mHeight << endl;
	}
public:
	string mName;
	int mAge;
	int mHeight;
};

bool compare_func(const Person &p1,const Person &p2)
{
	if (p1.mAge == p2.mAge)
	{
		// 年龄相同 按照身高进行排序,身高是降序
		return p1.mHeight > p2.mHeight;
	}
	else
	{
		return p1.mAge < p2.mAge; // 年龄是降序
	}
}

void test_01(void)
{
	list<Person> L;
	Person p1 = Person("张三", 10, 20);
	Person p2 = Person("李四", 10, 30);
	Person p3 = Person("王五", 20, 30);
	Person p4 = Person("二麻子", 20, 40);
	Person p5 = Person("张三", 30, 20);
	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);
	L.push_back(p5);

	L.sort(compare_func);

	list<Person>::iterator it = L.begin();
	for (it = it; it != L.end(); it++)
	{
		it->show_person();
	}
}

int main()
{

	test_01();
	system("pause");
	return 0;
}

排序结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值