list的使用

前言:以下使用是基于与string和vector部分较不常见的使用(少部分经常使用的未列举),还有部分可看list - C++ Reference来阅读文档

构造函数(Construct list)

cpp 复制代码
//打印函数
template<class Contain>
void Print(const Contain& con)
{
	for (auto& e : con)
	{
		cout << e << ' ';
	}
	cout << endl;
	cout << endl;
}

void test01()
{
	list<int> l1{ 1, 23, 4, 5, 6, 7 };//内存池构造
	Print(l1);
	list<int> l2(10, 200);//构造10个相同200
	Print(l2);
	cout << endl;
	cout << endl;
	list<int> l3(l2.begin(), l2.end());//按l2迭代器构造
	Print(l3);
	cout << endl;
	cout << endl;
	list<int> l4(l3);//拷贝l3
	Print(l4);
}

元素访问( Element access )

front

说明:访问首元素元素

cpp 复制代码
void test02()
{
	list<int> l1{ 1, 23, 4, 5, 6, 7 };
	cout << l1.front() << endl;//返回头元素
}

back

说明:访问末尾元素

cpp 复制代码
void test02()
{
	list<int> l1{ 1, 23, 4, 5, 6, 7 };
	cout << l1.back() << endl;//返回尾部元素
}

修饰(Modifiers)

assign

说明:将list类的实例化对象替换未一个新的list初始化对象

cpp 复制代码
//assign
	list<int> l2{ 1,2,3,4,5,6 };
	cout << "assign前:";
	Print(l2);
	//l2.assign(l1.begin()+1, l1.end());//error这是一个例子,双向迭代器不能支持+-操作,一般能实现的只有随机迭代器
	//l2.assign(l1.begin(), l1.end());//这是可以的
	l2.assign(10, 1000);
	cout << "assign后:";
	Print(l2);	

关于迭代器可以参考这个博客List迭代器和模拟(迭代器的模拟)-CSDN博客。

push_front

说明:在链表第一个结点头插

cpp 复制代码
//push_front
    list<int> l3{ 1,2,3,4,5 };
    cout << "push_front前:";
    Print(l3);
    cout << "push_front后:";
    l3.push_front(10000);
    Print(l3);

pop_front

说明:删除链表第一个元素结点

cpp 复制代码
//pop_front
    l3.pop_front();
    cout << "pop_front后:";
    Print(l3);

push_back和pop_back

说明:push_back在链表尾部插入结点;pop_back删除尾部结点

cpp 复制代码
	//push_back和pop_back
	list<int> l4{ 1,2,3,4,5 };
	cout << "push_back前:";
	Print(l4);
	l4.push_back(10000);
	cout << "push_back后";
	Print(l4);
	cout << "pop_back后:";
	l4.pop_back();
	Print(l4);

insert

说明:在任意位置插入结点

cpp 复制代码
	list<int> l5{ 1,2,3,4,5 };
	cout << "insert前:";
	Print(l5);
	l5.insert(l5.begin(), 10000);//同理被迭代器限制,不能加加(解决办法就是在调用之前使用一个变量存储迭代器然后++\--操作达到想要的指向地址)
	cout << "insert后:";
	Print(l5);

erase

说明:在任意位置删除结点

cpp 复制代码
	cout << "erase后:";
	l5.erase(l5.begin());
	Print(l5);

数据操作(Operations)

remove

说明:删除所有数据未val的结点

cpp 复制代码
	//remove删除所有val数据
	list<int> l2{ 1,2,2,2,2,2,10,20,30,30 };
	cout << "remove前:";
	Print(l2);
	cout << "remove后:";
	l2.remove(2);
	Print(l2);

unique

说明:删除重复的元素

cpp 复制代码
	//unique删除重复的数
	list<int> l3{ 1,2,2,2,2,2,10,20,30,30 };
	cout << "删除重复前:";
	Print(l3);
	l3.unique();
	cout << "删除重复后:";
	Print(l3);

sort

说明:排序未有序序列(升序)

cpp 复制代码
	list<int> l4{ 20,10,30,40,70,50,80,60 };
	list<int> l5{ 100,500,300,600,200,900,400 };
	l4.sort();
	l5.sort();

merge

说明:合并有序链表(一般需要和sort使用)

cpp 复制代码
	//merge合并到调用merge的链表中(内部必须是有序序列,所以在使用之前都要使用sort函数)
	list<int> l4{ 20,10,30,40,70,50,80,60 };
	list<int> l5{ 100,500,300,600,200,900,400 };
	l4.sort();
	l5.sort();
	cout << "merge前:";
	Print(l4);
	cout << "merge后:";
	l4.merge(l5);
	Print(l4);

reserve

说明:对链表进行逆置处理

cpp 复制代码
	//reverse逆置
	list<int> l6{ 1,2,3,4,5,6,7,8,9,0 };
	cout << "逆置前:";
	Print(l6);
	l6.reverse();
	cout << "逆置后:";
	Print(l6);

splice

说明:拼接2个list链表

cpp 复制代码
#include <iostream>
#include <list>

int main() {
std::list<int> l1 = {1, 2, 3};
std::list<int> l2 = {4, 5};

l1.splice(l1.begin(), l2);

std::cout << "list l1 after splice operation: ";
for (auto ele : l1) std::cout << ele << " ";
std::cout << std::endl;

std::cout << "list l2 after splice operation: ";
std::cout << "l2.size is: " << l2.size() << std::endl;

return 0;
}

拼接后会使得被拼接的链表结点清除。

解释一下文档中第二个和第三个,第二个是从pos位置拼接从另一个list的i位置开始

第三个是从pos位置拼接从另一个list的迭代器拼接。

相关推荐
nice_lcj5203 小时前
LRU 缓存入门:它是什么、怎么用、面试怎么手写
java·数据结构·缓存·面试·lru
꧁细听勿语情꧂4 小时前
直接选择排序,堆排序,冒泡排序,递归快速排序、hoare版本、挖坑法、lomuto前后指针,非递归快速排序
c语言·开发语言·数据结构·算法
Pniubi16 小时前
关于算法用现成数据结构和手撕
数据结构
老师我太想进步了20261 天前
【数据结构-6】算法美学之单链表(1)
数据结构·算法
希望我万事顺遂1 天前
力扣hot100-滑动窗口最大值
数据结构·算法·leetcode
꧁细听勿语情꧂1 天前
二叉树余题,排序、直接插入排序、希尔排序
c语言·开发语言·数据结构·算法
是main不是漫1 天前
【双指针|LeetCode1089 复写零】巧用逆向思路,破解原地覆盖陷阱
数据结构·算法·leetcode
饮水桶_T2 天前
JAVA IO流笔记
开发语言·数据结构·算法
wo_jack2 天前
C++ 入门笔记(十七):list 入门 —— 为什么 list 不能 sort?
数据结构·c++·面试·stl·list
zaizai10072 天前
算法(左程云)1
数据结构