【C++ list容器用法详解】

构造函数(包含赋值)

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include<list>
using namespace std;

//链表与数组相比的优缺点
//优点:更快的进行插入和删除,链表在进行插入和删除时只需要修改指针,而数组需要移动大量元素
//缺点:1.容器遍历速度慢,数组中各元素连续,遍历速度比容器快  2.占用空间比数组大

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

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

void test01()
{
	list<int>L;

	//添加数据
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	L.push_back(40);

	//遍历容器
	ListPrint(L);

	//区间方式构造
	list<int>L2(L.begin(), L.end());

	//拷贝方式构造
	list<int>L3(L2);
	 
	//n个elem    10个100
	list<int>(10, 100);
}

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

插入和删除

#define _crt_secure_no_warnings 1
#include <iostream>
#include<list>
using namespace std;

void ListPrint(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);
	//遍历
	ListPrint(L1);

	//头删
	L1.pop_front();
	ListPrint(L1);

	//尾删
	L1.pop_back();
	ListPrint(L1);

	//在开头插入数据
	L1.insert(L1.begin(), 10);
	ListPrint(L1);
	list<int>::iterator it = L1.begin();
	L1.insert(++it, 11);//使用迭代器使list容器的插入不仅仅只是头插和尾插,而可以是中间任意位置

	//在结尾插入数据
	L1.insert(L1.end(), 40);
	ListPrint(L1);

	//任意位置删除  比如我们想删除刚刚插入的11
	it = L1.begin();//修改it位置,使他等于11所在位置
	L1.erase(++it);
	ListPrint(L1);

	//list容器还可以批量删除
	L1.push_back(10);
	L1.push_back(10);//加上之前的一个10,现在容器中有三个10
	ListPrint(L1);

	L1.remove(10);//删除容器中所有10
	ListPrint(L1);
	
}

大小操作

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include<list>
using namespace std;

void ListPrint(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_back(50);

	ListPrint(L1);

	//判断容器是否为空
	if (L1.empty())
	{
		cout << "L1为空!" << endl;
	}
	else
	{
		cout << "L1不为空!" << endl;
	}
	
	//求大小(元素个数)
	cout <<"L1的大小:"<< L1.size() << endl;

	//重新指定大小
	L1.resize(10, 66);
	cout << "重新指定大小后:" << endl;
	ListPrint(L1);
}

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

数据存取

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include<list>
using namespace std;

void test01()
{
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//list容器的本质是链表,不是连续性空间存储,不支持随机访问
	//不可以使用[]或者at这两种方式访问list容器

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

	//list容器和其他容器不同,因为他的本质是链表的原因

	list<int>::iterator it = L1.begin();
	it++;
	it--;
	/*it += 1;*///编译器报错,不支持随机访问,支持双向访问的容器,这种验证方法同样适用于其他容器
}

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

反转和排序

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include<list>
#include<algorithm>
using namespace std;

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

bool Mysort(int &l1, int &l2)
{
	return l1 > l2;
}

void test01()
{
	list<int>L1;

	L1.push_back(10);
	L1.push_back(40);
	L1.push_back(30);
	L1.push_back(50);
	L1.push_back(20);
	cout << "互换前:";
	ListPrint(L1);

	//对位互换
	L1.reverse();
	cout << "互换后:";
	ListPrint(L1);

	//排序
    /*sort(L1.begin(),L1.end());*/
//因为list容器本质上是链表的特殊原因,不能使用之前我们学到的排序方法,list有自己的排序方法
	L1.sort();//默认是升序
	cout << "升序排序后:" << endl;
	ListPrint(L1);

	cout << "降序排序后:" << endl;
	L1.sort(Mysort);
	ListPrint(L1);
}

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

排序案列

创建一个类,里面存放若干个人的姓名,年龄以及身高,利用list容器对其进行排序。

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

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include<list>
#include<string>
using namespace std;

//排序方法,按照年龄进行升序,年龄相同则按照身高进行降序
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 MyPersonSort(Person &p1, Person &p2)
{
	//如果年龄相等
	if (p1.m_Age == p2.m_Age)
	{
		return p1.m_Height>p2.m_Height;
	}
	//年龄升序
	return p1.m_Age < p2.m_Age;
}

void test01()
{
	list<Person>L1;

	Person p1("张三", 100, 120);
	Person p2("李四", 20, 170);
	Person p3("王五", 20, 180);
	Person p4("赵六", 30, 176);
	Person p5("孙七", 30, 175);
	Person p6("周八", 40, 165);

	L1.push_back(p1);
	L1.push_back(p2);
	L1.push_back(p3);
	L1.push_back(p4);
	L1.push_back(p5);
	L1.push_back(p6);

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

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

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

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

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 23
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值