《STL 六大组件之容器篇:简单了解 list》

目录

  • [一、list 简介](#一、list 简介)
  • [二、list 的常用接口](#二、list 的常用接口)
    • [1. 构造函数(constructor )](#1. 构造函数(constructor ))
    • [2. 迭代器(iterator)](#2. 迭代器(iterator))
    • [3. 容量、修改和访问(capacity 、modify and access)](#3. 容量、修改和访问(capacity 、modify and access))

一、list 简介

简单来说,list 就是数据结构初阶中学习的链表,还是所有特性都具备的带头双向循环链表。带头是为了更好地适应迭代器,双向循环是为了插入和删除的效率。与之前学习的 list 相比,本次学习的 list 升级成为了类模板且增加了迭代器。

二、list 的常用接口

下面介绍一下 list 各方面的常用接口。

1. 构造函数(constructor )

下面是 list 常用的四个构造函数的声明和使用。

(1)函数声明

cpp 复制代码
// list 构造函数声明

// 1. 默认构造函数
list();
// 2. 指定个数和初始值
list(size_t n, const T& value = T());
// 3. 迭代器构造函数
template<class Iterator>
list(Iterator first, Iterator last);
// 4. 复制构造函数
list(const list<T>& lt);

(2)使用演示

cpp 复制代码
// 1. constructor
void test1()
{
	// 1. 默认构造函数
	list<int> lt1;
	cout << "lt1.size: " << lt1.size() << endl << endl;
	
	// 2. 指定个数和初始值
	list<int> lt2(5, 1);
	cout << "lt2.size: " << lt2.size() << endl;
	cout << "lt2: ";
	for (const auto& e : lt2)
		cout << e << " ";
	cout << endl << endl;

	// 3. 迭代器构造函数
	vector<int> vt_i;
	for (int i = 1; i < 5; ++i)
		vt_i.push_back(i);
	list<int> lt3(vt_i.begin(), vt_i.end());
	cout << "lt3.size: " << lt3.size() << endl;
	cout << "lt3: ";
	for (const auto& e : lt3)
		cout << e << " ";
	cout << endl << endl;

	// 4. 拷贝构造函数
	list<int> lt4(lt3);
	cout << "lt4.size: " << lt4.size() << endl;
	cout << "lt4: ";
	for (const auto& e : lt4)
		cout << e << " ";
	cout << endl << endl;
}

(3)运行结果

2. 迭代器(iterator)

下面介绍 list 常用的四个迭代器。反向迭代器参考正向迭代器的用法。

(1)函数声明

cpp 复制代码
// 1. 普通迭代器
iterator begin();
iterator end();
// 2. const 迭代器
const_iterator begin() const;
const_iterator end() const;
// 3. 反向迭代器
reverse_iterator rbegin();
reverse_iterator rend();
// 4. const 反向迭代器
const_reverse_iterator rbegin() const;
const_reverse_iterator rend() const;

(2)使用演示

cpp 复制代码
// 2. 迭代器
void test2()
{
	list<int> lt1;
	for (int i = 1; i < 10; ++i)
		lt1.push_back(i);
	// 1. 正向迭代器遍历
	list<int>::iterator it = lt1.begin();
	while (it != lt1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	// 2. 反向迭代器遍历
	list<int>::reverse_iterator rit = lt1.rbegin();
	while (rit != lt1.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
}

(3)运行结果

3. 容量、修改和访问(capacity 、modify and access)

下面分别介绍 list 的 2 个与容量有关的接口、2 个与访问有关的接口,8 个与修改有关的接口。

(1)函数声明

下面的 T 是模版中的类型参数。

cpp 复制代码
// 1. capacity
size_t size() const;
bool empty() const;

// 2. access
T& front();
T& back();

// 3. modify
void push_front(const T& value);
void push_back(const T& value);
void pop_front();
void pop_back();
iterator insert(iterator pos, const T& value);  // 在 pos 位置前插入
iterator erase(iterator pos);  // 删除 pos 位置

(2)使用演示

cpp 复制代码
// 3. capacity、access and modify
void test3()
{
	// 1. capacity
	list<int> lt1;
	if (lt1.empty())
	{
		cout << "lt1 is empty.\n";
	}
	for (int i = 1; i < 10; ++i)
		lt1.push_back(i);
	cout << "lt1.size: " << lt1.size() << endl << endl;

	// 2. access
	cout << "lt1.front: " << lt1.front() << endl;
	cout << "li1.back: " << lt1.back() << endl;

	// 3. modify
	list<int> lt2;
	// 插入
	lt2.push_back(1);
	lt2.push_front(2);
	// 打印
	for (const auto& e : lt2)
		cout << e << " ";
	cout << endl;
	// 插入
	lt2.insert(lt2.begin(), 10);
	lt2.insert(lt2.end(), 99);
	// 打印
	for (const auto& e : lt2)
		cout << e << " ";
	cout << endl;
	// 删除
	lt2.pop_back();
	lt2.pop_front();
	// 打印
	for (const auto& e : lt2)
		cout << e << " ";
	cout << endl;
	// 删除
	lt2.erase(lt2.begin());
	lt2.erase(--lt2.end());
	// 打印
	for (const auto& e : lt2)
		cout << e << " ";
	cout << endl;
}

(3)运行结果

相关推荐
uyeonashi4 分钟前
【Boost搜索引擎】构建Boost站内搜索引擎实践
开发语言·c++·搜索引擎
再睡一夏就好7 分钟前
从硬件角度理解“Linux下一切皆文件“,详解用户级缓冲区
linux·服务器·c语言·开发语言·学习笔记
TIF星空1 小时前
【使用 C# 获取 USB 设备信息及进行通信】
开发语言·经验分享·笔记·学习·microsoft·c#
Smile丶凉轩3 小时前
Qt 界面优化(绘图)
开发语言·数据库·c++·qt
reasonsummer4 小时前
【办公类-100-01】20250515手机导出教学照片,自动上传csdn+最大化、最小化Vs界面
开发语言·python
small_wh1te_coder5 小时前
从经典力扣题发掘DFS与记忆化搜索的本质 -从矩阵最长递增路径入手 一步步探究dfs思维优化与编程深度思考
c语言·数据结构·c++·stm32·算法·leetcode·深度优先
苏三福5 小时前
ros2 hunmle bag 数据包转为图片数据 python版
开发语言·python·ros2humble
qqxhb6 小时前
零基础学Java——第十一章:实战项目 - 桌面应用开发(JavaFX入门)
java·开发语言·javafx
大神薯条老师6 小时前
Python零基础入门到高手8.4节: 元组与列表的区别
开发语言·爬虫·python·深度学习·机器学习·数据分析
z人间防沉迷k6 小时前
堆(Heap)
开发语言·数据结构·笔记·python·算法