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

相关推荐
lqjun082711 小时前
Qt程序单独运行报错问题
开发语言·qt
hdsoft_huge13 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘13 小时前
39.网络流入门
开发语言·网络·c++·算法
未来之窗软件服务14 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
混分巨兽龙某某14 小时前
基于Qt Creator的Serial Port串口调试助手项目(代码开源)
c++·qt creator·串口助手·serial port
小冯记录编程14 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
1uther14 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
C_Liu_15 小时前
C++:类和对象(下)
开发语言·c++
coderxiaohan15 小时前
【C++】类和对象1
java·开发语言·c++
阿昭L15 小时前
MFC仿真
c++·mfc