list(链表)容器的规则及list的高级排序案例

1.list的基本概念:

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

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

链表是由一系列节点组成,节点的组成包含存储数据元素的数据域和存储下一个节点地址的指针域

STL中的链表是一个双向循环链表

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

list的优点:采用动态存储分配,不会造成内存浪费和溢出

链表执行插入和删除操作十分方便,修改指针即可,不需要像容器那样移动大量元素

list的缺点:链表更灵活,但空间(指针)和时间(遍历)额外耗费较大

list的重要性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector中是不成立的(vector插入元素大于开始容量时,会进行深拷贝,原有迭代器会失效)

注意:STL中list和vector容器是最常被使用的容器,各有优缺点

 2.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 test01()//list的操作
{
	//1
	list<int> L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	L1.push_back(50);
	L1.push_front(5);
	Print_List(L1);
	//2
	list<int> L2(L1.begin(), L1.end());
	Print_List(L2);
	//3
	list<int> L3(5, 100);
	Print_List(L3);
	//4
	list<int> L4(L3);
	Print_List(L4);
}

 3.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 test01()//list的赋值操作
{
	list<int> L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	L1.push_back(50);
	L1.push_front(5);
	Print_List(L1);
	//1
	list<int> L2;
	L2.assign(L1.begin(), L1.end());
	Print_List(L2);
	//2
	list<int> L3;
	L3.assign(5, 100);
	Print_List(L3);
	//3
	list<int> L4;
	L4 = L3;
	Print_List(L4);
}
void test02()//list的交换操作
{
	list<int> L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	L1.push_back(50);
	L1.push_front(5);
	list<int> L2(5, 100);
	cout << "交换前:" << endl;
	Print_List(L1);
	Print_List(L2);
	L1.swap(L2);
	cout << "交换后:" << endl;
	Print_List(L1);
	Print_List(L2);
}

4.list的大小操作

 5.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 test01()//list的插入和删除操作
{
	list<int> L1;
	//尾插
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	Print_List(L1);
	//头插
	L1.push_front(100);
	L1.push_front(100);
	L1.push_front(100);
	Print_List(L1);
	//头删
	L1.pop_front();
	Print_List(L1);
	//尾删
	L1.pop_back();
	Print_List(L1);
	//insert插入
	list<int>::iterator it = L1.begin();
	L1.insert(++it, 50);//第一个参数是迭代器,先++在插入//注意只能用++不能用+n
	Print_List(L1);
	//删除
	it = L1.begin();
	L1.erase(++it);
	Print_List(L1);
	//移除,删除list中所有为m的元素
	L1.push_back(1000);
	L1.push_back(1000);
	L1.push_back(1000);
	L1.push_back(1000);
	Print_List(L1);
	L1.remove(1000);
	Print_List(L1);
	//清空
	L1.clear();
	Print_List(L1);
}

6.list的数据存取

不能用[]和at()访问,因为数据的内存地址不相连,不支持随机访问

it(迭代器)=it+1报错说明不支持随机访问

it++和it--报错一个说明说明单向链表,都不报错说明是双向链表

 7.list的反转和排序

reserve是预留空间

reverse是反转

 

void Print_List(const list<int> L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
bool myCompare(int a, int b)//比较函数
{
	return a > b;//返回(第一个数大于第二个数相当于降序)反之升序
}
void test01()
{
	list<int> L1;
	//尾插
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	Print_List(L1);
	L1.reverse();
	cout << "反转后" << endl;
	Print_List(L1);
	L1.push_front(50);
	L1.push_front(70);
	L1.push_front(60);
	Print_List(L1);
	cout << "排序后:" << endl;
	L1.sort();//不支持随机访问迭代器的容器里,不能用标准算法//但内部会提供一些算法
	//默认排序为升序
	Print_List(L1);
	L1.sort(myCompare);//降序
	cout << "降序后:" << endl;
	Print_List(L1);
}

8.高级排序的案例(对于自定义类型数据,必须自己制定排序规则进行高级排序)

 

class Person
{
public:
	Person(string name,int age,int height)
	{
		this->m_name = name;
		this->m_age = age;
		this->m_height = height;
	}
	string m_name;
	int m_age;
	int m_height;
};
void Print_List(const list<Person> L)
{
	for (list<Person>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << " 身高:" << it->m_height << endl;
	}
	cout << endl;
}
bool myCompare(Person p1,Person p2)//比较函数
{
	if (p1.m_age == p2.m_age)//排序规则:优先按年龄升序,相同则身高降序
	{
		return p1.m_height > p2.m_height;
	}
	else
	{
		return p1.m_age < p2.m_age;
	}
	//return (p1.m_age == p2.m_age ? p1.m_height > p2.m_height:p1.m_age < p2.m_age);//三目运算符
}
void test01()
{
	Person p1("刘备", 30, 175);
	Person p2("张飞", 33, 175);
	Person p3("关羽", 30, 185);
	Person p4("赵云", 34, 176);
	Person p5("马超", 37, 178);
	list<Person> L;
	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);
	L.push_back(p5);
	Print_List(L);
	cout << "-------------------------------" << endl;
	cout << "排序后:" << endl;
	L.sort(myCompare);
	Print_List(L);
}

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值