list(链表)容器(二)

一、list 插入和删除

函数原型:

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

代码示例:

#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 test()
{
	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);

	//300 200 100 10 20 30
	printList(L);


	//尾删	300 200 100 10 20
	L.pop_back();
	printList(L);

	//头删	200 100 10 20
	L.pop_front();
	printList(L);

	//insert插入	200 1000 100 10 20
	list<int>::iterator it = L.begin();
	L.insert(++it,1000);
	printList(L);

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

	//移除
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	printList(L);
	L.remove(10000);//删除所有
	printList(L);

	//清空
	L.clear();
	printList(L);
}

int main()
{
	test();
	return 0;
}

总结:

尾插 --- push_back        尾删 --- pop_back

头插 --- push_front        头删 --- pop_front

插入 --- insert                 删除 --- erase

移除 --- remove              清空 --- clear  

二、list 数据存取

函数原型:

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

代码示例:

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

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

	//L1[0]	不可以用[]访问list容器中的元素

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

	//原因是list本质为链表,不适用连续线性空间存储数据,迭代器也是不支持随机访问的
	
	cout << "第一个元素为:" << L1.front() << endl;
	cout << "最后一个元素为:" << L1.back() << endl;

	//验证迭代器是不支持随机访问的
	list<int>::iterator it = L1.begin();
	it++;//it--,支持双向,但写成it=it+1则出错,不支持随机访问
}

int main()
{
	test();
	return 0;
}

总结:

list容器中不可以通过[]或者at方式访问数据

返回第一个元素 --- front

返回最后一个元素 --- back

三、list 反转和排序

函数原型:

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

代码示例:

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

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(20);
	L1.push_back(10);
	L1.push_back(50);
	L1.push_back(40);
	L1.push_back(30);

	cout << "反转前:" << endl;
	printList(L1);

	//反转
	L1.reverse();
	cout << "反转后:" << endl;
	printList(L1);
}

bool myCompare(int v1,int v2)
{
	//降序 就让第一个数 > 第二个数
	return v1 > v2;
}

//排序链表
void test02()
{
	list<int>L1;
	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(50);
	L1.push_back(40);
	L1.push_back(30);

	//排序
	cout << "排序前:" << endl;
	printList(L1);

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

	L1.sort();//默认排序规则 从小到大 升序
	cout << "排序后:" << endl;
	printList(L1);

	L1.sort(myCompare);
	printList(L1);
}

int main()
{
	//test01();
	test02();
	return 0;
}

总结:

反转 --- reverse

排序 --- sort (成员函数)

四、排序案例

案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高

排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序

代码示例:

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

//list容器 排序案例
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;//身高	
};

//指定排序规则
bool comparePerson(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;
	}
}

void test()
{
	//创建容器
	list<Person>L;

	//准备数据
	Person p1("刘备", 35, 175);
	Person p2("曹操", 45, 180);
	Person p3("孙权", 40, 170);
	Person p4("赵云", 25, 190);
	Person p5("张飞", 35, 160);
	Person p6("关羽", 35, 200);

	//插入数据
	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);
	L.push_back(p5);
	L.push_back(p6);

	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高:" << (*it).m_Height << endl;
	}

	//排序
	cout << "----------------------------" << endl;
	cout << "排序后:" << endl;

	L.sort(comparePerson);
	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高:" << (*it).m_Height << endl;
	}
}

int main()
{
	test();
	return 0;
}

总结:

对于自定义数据类型,必须要指定排序规则,否则编译器不知道如何进行排序

高级排序只是在排序规则上再进行一次逻辑规则制定,并不复杂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值