《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)运行结果

相关推荐
qq_5703985711 分钟前
Three.js基础使用-案例
开发语言·javascript·ecmascript
Suxing916 分钟前
C语言基础分享:C语言文件操作:从“写日记”到“拍电影”的存盘指南
c语言·开发语言
右耳朵猫AI20 分钟前
Node.js周刊2026W36 | NestJS 12发布、Remix 3 RC、pnpm 12 Rust重写、Node.js 26.8.0
开发语言·rust·node.js
啦啦啦啦啦zzzz31 分钟前
Appender抽象类的实现c++20
linux·服务器·c++·c++20
统计学小王子37 分钟前
数学建模国赛倒计时 2 天 ——《异常值的处理(R语言)》
开发语言·数学建模·r语言
z落落3 小时前
C# WinForm Socket 转串口设备服务器+Modbus CRC16校验+Socket 客户端
服务器·开发语言·c#
小灰灰搞电子9 小时前
Rust+Slint 实现动态消息提示框源码分享
开发语言·后端·rust
虹科数字化与AR9 小时前
拓影Twyn能替代三坐标吗?AR视觉检查与精密计量的正确分工
经验分享
神仙别闹10 小时前
基于 C++ 实现两个有序链表序列的交集
java·c++·链表
传奇开心果编程11 小时前
【Rust入门知识点学与练】第21课:Trait 进阶 Advanced Traits
开发语言·学习·rust