C++进阶学习:STL常用容器--list容器

发布于:2025-05-09 ⋅ 阅读:(14) ⋅ 点赞:(0)

1. list 基本概念

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

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

链表的组成:链表由一系列结点组成

结点的组成:一个是存储数据元素的数据域  另一个是存储下一个结点地址的指针域

优点:

  可以对任意位置进行快速的插入或删除操作

  采用动态存储分配  不会造成内存浪费和溢出

缺点:

  遍历速度比数组慢 

  占用空间大

list 容器的重要性质:插入和删除操作都不会造成原有 list 容器迭代器的失效

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

2. list 构造函数

功能描述:

创建 list 容器

与其他常用容器类似

代码示例

#include <iostream>
using namespace std;
#include <string>
#include <list>
#include <algorithm>  //标准算法头文件

//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);  //10 20 30 40

	//区间方式构造
	list<int> L2(L1.begin(),L1.end());  
	printList(L2);  //10 20 30 40

	//拷贝构造
	list<int> L3(L1);
	printList(L3);  //10 20 30 40

	//n个elem
	list<int> L4(3, 100);
	printList(L4);  //100 100 100
}

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

3. list 交换和赋值

功能描述:

给 list 容器赋值和进行交换操作
 

代码示例

#include <iostream>
using namespace std;
#include <string>
#include <list>
#include <algorithm>  //标准算法头文件

//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);  //10 20 30 40

	list<int> L2;
	L2 = L1;
	printList(L2);  //10 20 30 40

	list<int> L3;
	L3.assign(L1.begin(), L1.end());
	printList(L3);  //10 20 30 40

	list<int> L4;
	L4.assign(3, 100);
	printList(L4);  //100 100 100
}

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

	list<int> L2;
	L2.assign(3, 100);

	cout << "交换前:" << endl;
	printList(L1);  //10 20 30 40
	printList(L2);  //100 100 100
	cout << "交换后:" << endl;
	L1.swap(L2);
	printList(L1);  //100 100 100
	printList(L2);  //10 20 30 40
}

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

4. list 大小操作

功能描述:

对 list 容器的大小进行操作

代码示例

#include <iostream>
using namespace std;
#include <string>
#include <list>
#include <algorithm>  //标准算法头文件

//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);  //10 20 30 40

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

	L1.resize(10,5);
	printList(L1);  //10 20 30 40 5 5 5 5 5 5

	L1.resize(2);
	printList(L1);  //10 20
}

void test02()
{
}

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

5. list 中的插入和删除

功能描述:

对 list 容器进行插入和删除操作

remove  --  删除容器中所有与 elem 值匹配的元素

代码示例

#include <iostream>
using namespace std;
#include <string>
#include <list>
#include <algorithm>  //标准算法头文件

//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_front(100);
	L1.push_front(200);
	L1.push_front(300);

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

	//尾删
	L1.pop_back();
	printList(L1);  //300 200 100 10 20
	//头删
	L1.pop_front();
	printList(L1);  //200 100 10 20

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

	//删除
	it = L1.begin();
	L1.erase(it);
	printList(L1);  //55 100 10 20

	//移除
	L1.push_back(10000);
	L1.push_back(10000);
	L1.push_back(10000);
	printList(L1);  //55 100 10 20 10000 10000 10000
	L1.remove(10000);
	printList(L1);  //55 100 10 20

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

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

6. list 数据存取

功能描述:

对 list 容器中的元素进行存取

代码示例

#include <iostream>
using namespace std;
#include <string>
#include <list>
#include <algorithm>  //标准算法头文件

//list 数据存取

void test01()
{
	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 本质是链表  不是用连续线性空间存储数据  迭代器不支持随机访问

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

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

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

7. list 反转和排序

功能描述:

将容器中的元素反转以及将容器中的元素排序

所有不支持随机访问迭代器的容器  不可以使用标准算法
但是这些容器内部会提供对应的一些算法

代码示例

#include <iostream>
using namespace std;
#include <string>
#include <list>
#include <algorithm>  //标准算法头文件

//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(2);
	L1.push_back(1);
	L1.push_back(5);
	L1.push_back(4);
	L1.push_back(3);

	//反转
	cout << "反转前:" << endl;
	printList(L1);  //2 1 5 4 3

	L1.reverse();
	cout << "反转后:" << endl;
	printList(L1);  //3 4 5 1 2
}

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

void test02()
{
	list<int> L1;
	L1.push_back(2);
	L1.push_back(1);
	L1.push_back(5);
	L1.push_back(4);
	L1.push_back(3);

	//排序
	cout << "排序前:" << endl;
	printList(L1);  //2 1 5 4 3

	//所有不支持随机访问迭代器的容器  不可以使用标准算法
	//但是这些容器内部会提供对应的一些算法
	//sort(L1.begin(), L1.end());
	cout << "排序前:" << endl;
	L1.sort();
	printList(L1);  //1 2 3 4 5
	L1.sort(myCompare);
	printList(L1);  //5 4 3 2 1

}

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

8. 排序案例

案例描述:

将 Person 自定义数据类型进行排序  Person 中属性有姓名 年龄 身高

排序规则:

按照年龄进行升序  如果年龄相同按照身高进行降序

对自定义数据类型的排序  需要指定规则

高级排序只是在排序规则上再进行一次逻辑规则制定

代码示例

#include <iostream>
using namespace std;
#include <string>
#include <list>
#include <algorithm>  //标准算法头文件

//list 排序案例

class Person
{
public:

	Person(string name, int age, int heigh)
	{
		this->m_age = age;
		this->m_heigh = heigh;
		this->m_name = name;
	}
	string m_name;
	int m_age;
	int m_heigh;
};

//指定排序规则
bool comparePerson(Person& p1, Person& p2)
{
	//按年龄升序
	if (p1.m_age == p2.m_age)
	{
		//年龄相同  身高降序
		return p1.m_heigh > p2.m_heigh;
	}
	return p1.m_age < p2.m_age;
}

void test01()
{
	list<Person> L;

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

	//插入数据
	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>::const_iterator it = L.begin();it != L.end();it++)
	{
		cout << "姓名:" << (*it).m_name << " 年龄:" << (*it).m_age << " 身高:" << (*it).m_heigh << endl;
	}

	cout << "------------------------------" << endl;
	cout << "排序后" << endl;
	L.sort(comparePerson);
	for (list<Person>::const_iterator it = L.begin();it != L.end();it++)
	{
		cout << "姓名:" << (*it).m_name << " 年龄:" << (*it).m_age << " 身高:" << (*it).m_heigh << endl;
	}
	/*
	姓名:刘备 年龄:35 身高:175
	姓名:曹操 年龄:45 身高:180
	姓名:孙权 年龄:40 身高:170
	姓名:赵云 年龄:25 身高:190
	姓名:张飞 年龄:35 身高:170
	姓名:关羽 年龄:35 身高:195
	-----------------------------
	排序后
	姓名:赵云 年龄:25 身高:190
	姓名:关羽 年龄:35 身高:195
	姓名:刘备 年龄:35 身高:175
	姓名:张飞 年龄:35 身高:170
	姓名:孙权 年龄:40 身高:170
	姓名:曹操 年龄:45 身高:180
	*/
}

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

网站公告

今日签到

点亮在社区的每一天
去签到