【C++】list容器

1.list基本概念

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.list构造函数

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

#include<list>
//链表list容器构造函数

//输出list链表
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容器
	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,1000);
	printList(L4);
}
int main() 
{ 
	test01();
	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

3.list赋值和交换

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

//list容器的赋值和交互
#include<list>
//遍历list容器
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容器
	list<int>L1;
	//给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(L2.begin(), L2.end()); //按区间赋值
	printList(L3);

	list<int>L4;
	L4.assign(10, 1000); //n个elem
	printList(L4);
}
//交换
void test02()
{
	//创建list容器
	list<int>L1;
	//给L1赋值
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	list<int>L2;
	//给L2赋值
	L2.assign(10, 1000); //n个elem
	
	cout << "交换前:" << endl;
	printList(L1);
	printList(L2);

	L1.swap(L2); //交换
	cout << "交换后:" << endl;
	printList(L1);
	printList(L2);
}
int main() 
{ 
	test01();
	cout << endl;
	test02();
	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

4.list大小操作

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

//list容器大小操作
#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 test01()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);

	//判断L1是否为空
	if (L1.empty())
	{
		cout << "L1为空" << endl;
	}
	else
	{
		cout << "L1不为空" << endl;
		cout << "L1的大小为:" << L1.size() << endl;
	}

	//重新指定大小
	L1.resize(10);
	printList(L1);

	//默认值定为了1000
	L1.resize(15, 1000);
	printList(L1);

	L1.resize(5); //把多余的值删除
	printList(L1);
}

int main() 
{ 
	test01();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

5.list插入和删除

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

//list容器的插入和删除
#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 test01()
{
	list<int>L1;

	//尾插
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	//头插
	L1.push_front(10);
	L1.push_front(20);
	L1.push_front(30);
	//30 20 10 10 20 30 40
	printList(L1);

	//尾删
	L1.pop_back();
	//30 20 10 10 20 30
	printList(L1);
	//头删
	L1.pop_front();
	//20 10 10 20 30
	printList(L1);

	//insert插入
	L1.insert(L1.begin(), 1000);
	//1000 20 10 10 20 30
	printList(L1);

	//利用it来插入指定位置
	list<int>::iterator it = L1.begin();
	L1.insert(++it, 5, 20000);
	//1000 20000 20000 20000 20000 20000 20 10 10 20 30
	printList(L1);

	//删除
	it = L1.begin();
	L1.erase(++it);
	//1000 20000 20000 20000 20000 20 10 10 20 30
	printList(L1);

	//移除
	L1.remove(20000); //移除所有值为20000的数据
	//1000 20 10 10 20 30
	printList(L1);

	//清空
	//L1.erase(L1.begin(), L1.end()); 这种区间删除的方法也可以
	L1.clear();
	printList(L1);
}

int main() 
{ 
	test01();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

6.list数据存取

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

//list容器的数据存取
#include<list>
void test01()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);

	//L1[0]; //错误 不可以用[]访问list容器中的元素
	
	//L1.at(0); //错误 不可以用.at()方式访问list容器中的元素

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

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

	//list容器的迭代器是双向迭代器,不支持随机访问的
	list<int>::iterator it = L1.begin();
	it++;
	cout << *it << endl; //输出第二个元素:20
	it--;
	cout << *it << endl; //输出第一个元素:10
	//it = it + 1; //错误 不可以跳跃访问,即使是+1
}
int main()
{ 
	test01();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

7.list反转和排序

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

//list容器反转和排序
#include <list>

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

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

void test01()
{
	list<int>L;
	L.push_back(10);
	L.push_back(30);
	L.push_back(20);
	L.push_back(50);
	L.push_back(40);
	cout << "反转前:" << endl;
	//10 30 20 50 40
	printList(L);

	//反转
	L.reverse();
	cout << "反转后:" << endl;
	//40 50 20 30 10
	printList(L);

	//排序
	// sort(L.begin(), L.end()); 错误
	// 所有不支持随机访问迭代器的容器,不可以用标准算法algorithm
	// 不支持随机访问迭代器的容器,内部会提供对应一些算法
	//默认升序
	L.sort();  // 默认排序规则 从小到大 升序
	cout << "排序后:" << endl;
	printList(L);
	
	//降序
	L.sort(myCompare); // 降序,指定规则,从大到小
	printList(L);
}
int main()
{ 
	test01();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

8.排序案例

在这里插入图片描述

在这里插入图片描述

#include <iostream>
using namespace std;

//排序案例
#include <list>
#include <string>

//Person类 三国人物
class Person
{
public:
	Person(string name, int age, float height)
	{
		this->m_Name = name;
		this->m_Age = age;
		this->m_Height = height;
	}

	string m_Name;
	int m_Age;
	float m_Height;
};  //忘记加 ; 导致一直报错!! 相对于类的声明 例如: int a;

//遍历list
void printList(const list<Person>& L)
{
	for (list<Person>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << "\t年龄:"
			<< (*it).m_Age << "\t身高:" << it->m_Height << endl;
	}
}

//制定排序规则
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 test01()
{
	list<Person>L1; // 创建容器

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

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

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

	cout << "-------------------------" << endl;
	cout << "排序后:" << endl;
	
	//排序:
	L1.sort(comparePerson);
	printList(L1);
}

int main()
{ 
	test01();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值