【C++】list的使用

1. list的使用

首先,在使用list之前,我们得先了解list到底是个什么东西,查看文档可以了解到,list的底层是一个带头双向循环链表。

image-20230421153930048

按照顺序,我们首先来了解一下list的默认成员函数,这里我们不关注后面关于allocator的参数。

1. 构造函数

image-20230421154853012

接下来通过一段代码来了解一下:

void Test_construct()
{
	list<int> lt1;
	list<int> lt2(5, 2);
	list<int> lt3(lt2.begin(), lt2.end());
	list<int> lt4(lt3);
}

image-20230421155815223

2.迭代器的使用和数据访问

我们知道,迭代器设计模式的出现就是为了让所有的容器都有统一的访问方式,所以这里迭代器的使用与之前讲的string和vector没有任何区别。

void Test_Iterator()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	lt.push_back(6);
	//写
	list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		*it *= 10;
		++it;
	}
	//读
	it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

但是对于string和vector,我们对其中数据访问的方式有三种:[]下标访问,迭代器,范围for。其中使用[]访问的目的是为了访问到任意元素的时间复杂度都为O(1),但是对于list的结构:链表,我们,没办法做到O(1)的访问,所以这里就没有重载[]的必要了所以对于list的访问方式只有迭代器范围for两种。

3. 容量相关

1. size:拿到list的数据个数

由于list的结构,所以不会出现有容量的概念,因为每次插入数据或者删除数据的时候,直接插入或删除一个节点即可。所以这里只提供了size这个接口用于返回数据个数。

image-20230422133446094

2. resize:改变数据个数

image-20230422235713605

和其他STL容器的接口相同的用法

3. clear:清空容器中的所有数据

image-20230422235840390

void Test_Capacity()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	lt.push_back(6);
	cout << "size:" << lt.size() << endl;
	lt.resize(5);
	cout << "size:" << lt.size() << endl;
	lt.resize(10, 20);
	cout << "size:" << lt.size() << endl;
	auto begin = lt.begin();
	while (begin != lt.end())
	{
		cout << *begin << " ";
		++begin;
	}
	cout << endl;
	lt.clear();
	cout << "size:" << lt.size() << endl;
}

image-20230423000207920

4. 数据修改

由于链表结构的特殊性,我们可以很方便的头插,尾插或者在任意位置插入,所以在这里库里面对于数据插入提供了很多种方式

1.数据插入

1. push_back:尾插

image-20230422134237008

2. push_front:头插

image-20230422134217660

3.insert:在任意位置插入删除

image-20230422134302155

void Test_insert()
{
	vector<int> v(5, 888);
	list<int> lt;
	//尾插
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	lt.push_back(6);
	auto it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	//头插
	lt.push_front(10);
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	//在任意位置插入
	auto it_push = ++lt.begin();//从第二个位置开始
	lt.insert(it_push, 30);//插入一个值
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	++it_push;
	lt.insert(it_push, 5, 50);//插入n个值
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	++it_push;
	lt.insert(it_push, v.begin(), v.end());//插入一个迭代器区间
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
}

image-20230422135621556

2. 数据删除

与数据插入相对应的,数据删除也有三个

1. pop_back:尾删

image-20230422135837432

2.pop_front:头删

image-20230422135820204

3.erase:任意位置删除

image-20230422135902241

void Test_erase()
{
	list<int> lt;
	for (int i = 0; i < 10; ++i)
	{
		lt.push_back(i);
	}
	auto it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	//头删
	lt.pop_front();
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	//尾删
	lt.pop_back();
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	//任意位置删除
	auto pos = ++lt.begin();
	pos = lt.erase(pos);//删除某一位置
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	auto start = ++pos;
	auto end = ++(++start);
	lt.erase(start, end);//删除一个迭代器区间
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
}

image-20230422141427934

5.其他接口

除了上述的接口之外,还有一些我们之前在string和vector中没有见到的接口,下面我们来看看他们的用法

1. remove:删除list中指定值

image-20230423000514372

void Test_remove()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	lt.push_back(6);
	lt.push_back(7);
	lt.push_back(8);
	auto it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	lt.remove(5);
	lt.remove(10);
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
}

image-20230423001231333

可以看到,remove对于list中不存在的元素不会做任何操作

2. sort:排序list

image-20230423001550739

看到这里肯定会有人有疑惑,sort算法库里面不是实现过吗?为什么又要重新在list里实现一下,我直接用算法库里面的不行吗?

✅答案是:不行,接下来看实验==》

image-20230423002516367

可以看到,在编译的过程就已经报错了,这是因为库里面没有支持list迭代器类型的构造,为啥嘞?因为之前sort对容器内部的元素操作使用了+和-操作,但是list由于结构的限制,不支持迭代器的这个行为,所以对于list,要重新在库里面实现一个sort。

3. unique:删除list中的重复值

image-20230423003100595

这里注意一下,在使用unique之前要确保list是有序的,否则不能完成删除所有重复值的功能

void Test_Sort()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(10);
	lt.push_back(9);
	lt.push_back(3);
	lt.push_back(6);
	lt.push_back(3);
	lt.push_back(7);
	lt.push_back(6);
	cout << "原list:";
	auto it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	lt.unique();
	cout << "尝试在乱序的情况下使用unique:";
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	lt.sort();
	cout << "排序list:";
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
	cout << "对有序的list使用unique:";
	lt.unique();
	it_out = lt.begin();
	while (it_out != lt.end())
	{
		cout << *it_out << " ";
		++it_out;
	}
	cout << endl;
}

image-20230423004016031

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C语言不支持类模板的概念,但可以通过结构体和指针来实现一个类似于list的数据结构。 首先,我们可以定义一个包含数据和指向下一个节点的指针的结构体,例如: ```c typedef struct Node { int data; struct Node* next; } Node; ``` 然后,我们可以定义一个包含头指针和尾指针的结构体来表示这个list,例如: ```c typedef struct List { Node* head; Node* tail; } List; ``` 接下来,我们可以实现一些常见的操作,例如创建list、添加元素、删除元素等等。下面是一些示例代码: ```c void initList(List* list) { list->head = NULL; list->tail = NULL; } void addElement(List* list, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if (list->head == NULL) { list->head = newNode; list->tail = newNode; } else { list->tail->next = newNode; list->tail = newNode; } } void deleteElement(List* list, int data) { Node* current = list->head; Node* previous = NULL; while (current != NULL) { if (current->data == data) { if (previous == NULL) { list->head = current->next; } else { previous->next = current->next; } // 如果删除的是尾节点,更新tail指针 if (current == list->tail) { list->tail = previous; } free(current); return; } previous = current; current = current->next; } } ``` 通过以上代码,我们可以使用类似于list的数据结构来存储和操作元素。虽然这并不是真正意义上的类模板,但可以达到类似的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌云志.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值