C++ list容器

发布于:2022-12-04 ⋅ 阅读:(598) ⋅ 点赞:(0)

1. list基本概念

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

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

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

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

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

相对于数组,链表的优点、缺点:

        优点:可以对任意位置进行快速插入或删除

        缺点:容器遍历速度没有数组快,占用空间比数组大

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

list的优点:

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

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

list的缺点:

        链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大

        List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。

总结:STL中List和vector是两个最常被使用的容器,各有优缺点。

2. list构造函数

功能:创建list容器

函数原型:

 示例1:(默认构造)

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

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);
}

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

结果:

 

示例2:(区间构造)

 

 

拷贝构造:

list<int>L3(L2);

n个元素:

list<int> L4(10,1000);

3. list赋值和交换

功能:给list容器赋值,以及交换list容器

函数原型:

 示例1:(赋值)

等号赋值:

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

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);

	list<int> L2;
	L2 = L1;  //等号赋值
	printList(L2);
}

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

结果:

 

assign赋值:

list<int> L3;
L3.assign(L1.begin(), L1.end());
printList(L3);

 

list<int> L4;
L4.assign(10, 100);
printList(L4);

 

实例2:(交换)

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

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);

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

	//交换前
	printList(L1);
	printList(L2);

	//交换后
	L1.swap(L2);
	printList(L1);
	printList(L2);
}

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

结果: 

 

4. list大小操作

功能:对list容器的大小进行操作

函数原型:

 

5. list插入和删除

功能:对list容器进行数据的插入和删除

函数原型:

 

6. list数据存取

功能:对list容器中数据进行存取

函数原型:

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

L.at(0)   不可以用at方式访问list容器中的元素

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

7. list反转和排序

功能:将容器中的元素反转,以及将容器中的数据进行排序

函数原型:

L.reverse();  //list反转
L.sort();  //list排序

8. 排序案例

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

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

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

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 test01()
{
	//创建list容器
	list <Person> L;

	//准备数据
	Person p1("Bob", 35, 175);
	Person p2("Tom", 45, 180);
	Person p3("Nick", 20, 170);
	Person p4("Jarry", 20, 165);
	
	//向容器中插入数据
	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);

	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << " 年龄:" << it->m_Age << " 身高:" << it->m_Height << 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() {
	test01();
	system("pause");
	return 0;
}

结果:


网站公告

今日签到

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