STL容器 —— list 了解、接口使用,以及模拟实现list(部分常用接口)

news2024/10/6 0:35:47

注意 : 以下所有文档都来源此网站 : http://cplusplus.com/

一、vector的介绍及使用

list文档的介绍:https://cplusplus.com/reference/list/list/

1. vector 的介绍

1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。

2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。

3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。

4. 与其他的序列式容器相比(arrayvectordeque)list通常在任意位置进行插入、移除元素的执行效率更好。

5. 与其他序列式容器相比,listforward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)

二、list 常用接口说明

    1. list 的构造

           
构造函数( (constructor)
                     
接口说明
list (size_type n, const value_type& val = value_type())
构造的 list 中包含 n 个值为 val 的元素
list()
构造空的 list
list (const list& x)
拷贝构造函数
list (InputIterator fifirst, InputIterator last)
[first, last) 区间中的元素构造 list

    2. list 迭代器的使用

        迭代器可以理解为像指针一样,但可能是指针可能不是。

函数声明
接口说明
begin+ end
返回第一个元素的迭代器 + 返回最后一个元素下一个位置的迭代器
rbegin+ rend
返回第一个元素的 reverse_iterator, end 位置 返回最后一个元素下一个位置的 reverse_iterator, begin 位置

            

        (1) begin、end 是正向迭代器,对迭代器执行++操作,迭代器向后移动

        (2)rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动 

    3. list capacity

函数声明
接口说明
empty
检测 list 是否为空,是返回 true ,否则返回 false
size
返回 list 中有效节点的个数

    4. list element access

函数声明
接口说明
front
返回 list 的第一个节点中值的引用
back
返回 list 的最后一个节点中值的引用

    5. list modifiers

函数声明
接口说明
push_front
list 首元素前插入值为 val 的元素
pop_front
删除list中第一个元素
push_backlist尾部插入值为val的元素
pop_back删除list中最后一个元素
insertlist position 位置中插入值为val的元素
erase删除list position位置的元素
swap交换两个list中的元素
clear
清空 list 中的有效元素

三、常用接口的使用

        这里就简单使用一下,与前面的 vector string 的使用接口差别不大,这也就是 C++ 的封装的体现,底层都不一样,但是上层来看都是一样!

#include <iostream>
using namespace std;
#include <list>
#include <vector>


// list的构造
void TestList1()
{
    list<int> l1;                         // 构造空的l1
    list<int> l2(4, 100);                 // l2中放4个值为100的元素
    list<int> l3(l2.begin(), l2.end());  // 用l2的[begin(), end())左闭右开的区间构造l3
    list<int> l4(l3);                    // 用l3拷贝构造l4

    // 以数组为迭代器区间构造l5
    int array[] = { 16,2,77,29 };
    list<int> l5(array, array + sizeof(array) / sizeof(int));

    // 列表格式初始化C++11
    list<int> l6{ 1,2,3,4,5 };

    // 用迭代器方式打印l5中的元素
    list<int>::iterator it = l5.begin();
    while (it != l5.end())
    {
        cout << *it << " ";
        ++it;
    }       
    cout << endl;

    // C++11范围for的方式遍历
    for (auto& e : l5)
        cout << e << " ";

    cout << endl;
}


// list迭代器的使用
// 注意:遍历链表只能用迭代器和范围for
void PrintList(const list<int>& l)
{
    // 注意这里调用的是list的 begin() const,返回list的const_iterator对象
    for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
    {
        cout << *it << " ";
        // *it = 10; 编译不通过
    }

    cout << endl;
}

void TestList2()
{
    int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    list<int> l(array, array + sizeof(array) / sizeof(array[0]));
    // 使用正向迭代器正向list中的元素
    // list<int>::iterator it = l.begin();   // C++98中语法
    auto it = l.begin();                     // C++11之后推荐写法
    while (it != l.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    // 使用反向迭代器逆向打印list中的元素
    // list<int>::reverse_iterator rit = l.rbegin();
    auto rit = l.rbegin();
    while (rit != l.rend())
    {
        cout << *rit << " ";
        ++rit;
    }
    cout << endl;
}



// list插入和删除
// push_back/pop_back/push_front/pop_front
void TestList3()
{
    int array[] = { 1, 2, 3 };
    list<int> L(array, array + sizeof(array) / sizeof(array[0]));

    // 在list的尾部插入4,头部插入0
    L.push_back(4);
    L.push_front(0);
    PrintList(L);

    // 删除list尾部节点和头部节点
    L.pop_back();
    L.pop_front();
    PrintList(L);
}

// insert /erase 
void TestList4()
{
    int array1[] = { 1, 2, 3 };
    list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));

    // 获取链表中第二个节点
    auto pos = ++L.begin();
    cout << *pos << endl;

    // 在pos前插入值为4的元素
    L.insert(pos, 4);
    PrintList(L);

    // 在pos前插入5个值为5的元素
    L.insert(pos, 5, 5);
    PrintList(L);

    // 在pos前插入[v.begin(), v.end)区间中的元素
    vector<int> v{ 7, 8, 9 };
    L.insert(pos, v.begin(), v.end());
    PrintList(L);

    // 删除pos位置上的元素
    L.erase(pos);
    PrintList(L);

    // 删除list中[begin, end)区间中的元素,即删除list中的所有元素
    L.erase(L.begin(), L.end());
    PrintList(L);
}

// resize/swap/clear
void TestList5()
{
    // 用数组来构造list
    int array1[] = { 1, 2, 3 };
    list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
    PrintList(l1);

    // 交换l1和l2中的元素
    list<int> l2;
    l1.swap(l2);
    PrintList(l1);
    PrintList(l2);

    // 将l2中的元素清空
    l2.clear();
    cout << l2.size() << endl;
}

四、 模拟实现 list

        下面就来模拟实现一下 list,还是与以往一样建两个文件

        一个负责测试所模拟实现的接口文件(Test.cpp),一个负责实现接口的文件(list.hpp)

          1. 下面是负责测试所模拟实现的接口 Test.cpp文件的代码:

	void Test_list1()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		it = lt.begin();
		while (it != lt.end())
		{
			*it *= 2;
			cout << *it << " ";
			it++;
		}
		cout << endl;

		for (auto& e : lt)
		{
			cout << e*2 << " ";
		}
		cout << endl;
	}

	void Test_list2()
	{
		//std::list<int> lt;
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);


		auto pos = std::find(lt.begin(), lt.end(), 4);
		if (pos != lt.end())
		{
			lt.insert(pos, 55);

			//*pos *= 2;
		}
		cout << *pos << endl;

		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}


	void Test_list3()
	{
		//std::list<int> lt;
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		list<int>::reverse_iterator rit = lt.rbegin();
		while (rit != lt.rend())
		{
			cout << *rit << " ";
			rit++;
		}
		cout << endl;

	}


	void Test_list()
	{
		//std::list<int> lt;
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		list<int>::reverse_iterator rit = lt.rbegin();
		while (rit != lt.rend())
		{
			cout << *rit << " ";
			rit++;
		}
		cout << endl;

		auto pos = find(lt.begin(), lt.end(), 4);
		if (pos != lt.end())
		{
			pos = lt.insert(pos, 22);
			cout << *pos << endl;
		}
		
		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		auto ret = find(lt.begin(), lt.end(), 4);
		if (ret != lt.end())
		{
			lt.erase(ret);
		}

		it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}


}

          2. 下面是负责实现接口的文件 list.hpp 文件的代码:

#pragma once
#include <iostream>
#include <list>
#include <assert.h>
#include <algorithm>

#include "reverse_iterator.hpp"

using namespace std;

namespace HK
{
	template <class T>
	class list_node
	{
	public:
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;

		list_node(const T& x = T())
			:_data(x)
			,_next(nullptr)
			,_prev(nullptr)
		{}
	};

	template <class T, class Ref, class Ptr>
	class __list_iterator
	{
	public:
		typedef list_node<T> Node;
		typedef __list_iterator<T, Ref, Ptr> iterator;

		typedef bidirectional_iterator_tag iterator_category;
		typedef T value_type;
		typedef Ptr pointer;
		typedef Ref reference;
		typedef ptrdiff_t difference_type;

		__list_iterator(Node* node)
			:_node(node)
		{}
		
		bool operator!=(const iterator& it) const
		{
			return this->_node != it._node;
		}

		bool operator==(const iterator& it) const
		{
			return this->_node == it._node;
		}

		// 解引用 取数据
		//T& operator*()
		Ref operator*()
		{
			return this->_node->_data;
		}

		// 
		//T* operator->()
		Ptr operator->()
		{
			return &(this->operator*());
		}

		// 前置++
		iterator& operator++()
		{
			this->_node = this->_node->_next;

			return *this;
		}

		// 后置++
		iterator& operator++(int)
		{
			iterator ret(*this);

			this->_node = this->_node->_next;

			return ret;
		}


		// 前置--
		iterator& operator--()
		{
			this->_node = this->_node->_prev;

			return *this;
		}

		// 后置--
		iterator& operator--(int)
		{
			iterator ret(*this);

			this->_node = this->_node->_prev;

			return ret;
		}

	//private:
		Node* _node;
	};

	template <class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;

		typedef __reverse_iterator<iterator, T&, T*> reverse_iterator;
		typedef __reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;


		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end() const
		{
			return const_iterator(_head);
		} 

		const_reverse_iterator rbegin() const
		{
			return const_reverse_iterator(end());
		}

		const_reverse_iterator rend() const
		{
			return const_reverse_iterator(begin());
		}

		reverse_iterator rbegin() 
		{
			return reverse_iterator(end());
		}

		reverse_iterator rend() 
		{
			return reverse_iterator(begin());
		}

		list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		void push_back(const T& x)
		{
			//Node* cur = _head->_prev;
			//Node* newnode = new Node(x);
			//
			//_head->_prev = newnode;
			//newnode->_next = _head;
			//
			//newnode->_prev = cur;
			//cur->_next = newnode;

			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		iterator insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;

			Node* newnode = new Node(x);

			prev->_next = newnode;
			newnode->_prev = prev;

			newnode->_next = cur;
			cur->_prev = newnode;

			return iterator(newnode);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		//iterator erase(iterator pos)
		//{
		//	// 不能删头
		//	assert(pos != end());
		//	
		//	Node* cur = pos._node;
		//	Node* prev = cur->_prev;
		//	Node* next = cur->_next;

		//	prev->_next = next;
		//	next->_prev = prev;

		//	delete cur;

		//	return iterator(next);
		//}

		iterator erase(iterator pos)
		{
			assert(pos != end());

			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;
			delete cur;

			return iterator(next);
		}

	private:
		Node* _head;
	};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/514806.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

python自动化测试工具selenium使用指南 ,绝对能帮到你

目录 概述 pythonselenium环境安装 使用selenium启动浏览器 selenium页面加载等待和检测 使用time.sleep()等待 使用implicitly_wait设置最长等待时间 使用WebDriverWait设置等待条件 检测document是否加载完成 selenium元素定位和读取 查找元素 dom元素交互 查找元…

地址族和数据序列

IP地址 为使计算机连接到网络并收发数据,必需向其分配IP地址。 IP地址分为两类。 IPv4 (Internet Protocol version 4)4字节地址族 IPv6 (Internet Protocol version 6)16字节地址族 IPv4与IPv6的差别主要是表示IP地址所用的字节数,目前通用的地址族为IPv4。 IPv6是为了应对20…

全面接入!ChatGPT杀进15个商业应用,让AI替你打工

ChatGPT API开放60多天&#xff0c;世界已经不是两个月前的样子了。 微软联合创始人比尔盖茨称GPT是“40多年来最革命性的技术进步”&#xff0c;英伟达创始人黄仁勋高呼&#xff1a;“我们正处于AI的iPhone时刻”&#xff0c;阿里董事会主席兼CEO张勇也说&#xff1a;“所有行…

经验总结:(Git 版本控制工具快速入门)

Git学习笔记 版本控制(版本迭代) 什么是版本控制 版本迭代&#xff0c;开发过程中&#xff0c;对项目各个阶段的版本的控制。 在开发过程中用于管理我们对文件、目录或工程等内容的修改历史&#xff0c;方便查看更改历史记录&#xff0c;备份以便恢复以前的版本。 多人开发…

ADV7391BCPZ-ASEMI代理亚德诺ADV7391BCPZ原厂芯片

编辑-Z ADV7391BCPZ参数描述&#xff1a; 型号&#xff1a;ADV7391BCPZ VDD&#xff1a;1.8V VAA&#xff1a;3.3V 全驱动输出电流&#xff1a;34.6 mA 低驱动输出电流&#xff1a;4.3 mA 输出电容&#xff1a;10 pF 模拟输出延迟&#xff1a;6 ns DAC模拟输出倾斜&am…

x509证书-crl证书吊销

标准 CRL fields -- 版本、时间、证书序列号和扩展名 -- 都是在4.1节的ASN.1中定义的 -- AlgorithmIdentifier 在第 4.1.1.2 节中定义 以下各项描述了 X.509 v2 CRL 在 Internet PKI 中的使用。 关于签名值和验证 signatureValue 字段包含根据 ASN.1 DER 编码的 tbsCertList 计…

13-Vue技术栈之路由的使用

目录 1、路由相关理解1.1 vue-router 的理解1.2 对 SPA 应用的理解1.3 路由的理解1.4 路由分类 2、路由的基本使用2.1 实现效果&#xff1a;2.2 实现思路&#xff1a;2.3 实现步骤&#xff1a;2.4 具体代码2.5 几个注意点 3、多级路由&#xff08;嵌套路由&#xff09;4、路由的…

中科院学术专用版GPT Academic项目实现

【写在最前】要完成GPT 学术优化 (GPT Academic)这个项目需要一些值得注意的地方&#xff1a; chatGPT账户有余额且未过期&#xff01;&#xff01;&#xff01;有代理工具&#xff08;类似Clash&#xff09; 1、代码克隆 https://github.com/binary-husky/gpt_academic 根据…

Java后端调取微信小程序接口,创建微信小程序直播间

前提条件&#xff1a; 1.注册微信小程序 2.获取appId和secret秘钥 3.小程序具备直播权限 小程序直播开发文档网址 目录 1.创建和修改直播间 2.删除直播间 3.获取直播间分享二维码 1.创建和修改直播间 两个功能一起写&#xff0c;区别在于&#xff0c;修改的时候需要多一…

【数字化转型-05】数字化转型中战略驱动的利器——平衡计分卡

今年的4月19日&#xff0c; 华为第20届全球分析师大会上&#xff0c;华为孟晚舟在大会上发表了“初心如磐&#xff0c;奋楫笃行&#xff0c;共赢数字化未来”的主题演讲&#xff0c;其中分享了对数字化转型的三个核心洞见&#xff0c;首先&#xff0c;战略驱动是根本&#xff0…

Mojo:比 Python 快 35000 倍的 AI 编程语言

Mojo&#xff1a;比 Python 快 35000 倍的 AI 编程语言 Mojo是一门刚刚发布的面向 AI 开发人员的编程语言。 Mojo 被设计为 Python 的超集&#xff0c;所以如果你已经掌握了 Python&#xff0c;学习 Mojo 会很容易。关键是 Mojo 将 Python 的易用性与 C 语言的性能相结合&am…

AIGC—— 内容生产力革命的起点

作者简介&#xff1a;一名云计算网络运维人员、每天分享网络与运维的技术与干货。 座右铭&#xff1a;低头赶路&#xff0c;敬事如仪 个人主页&#xff1a;网络豆的主页​​​​​​ 目录 前言 一.AIGC 1.什么是AIGC? 2.AIGC有哪些优势与挑战 &#xff08;1&#xff0…

51单片机(十)DS1302实时时钟

❤️ 专栏简介&#xff1a;本专栏记录了从零学习单片机的过程&#xff0c;其中包括51单片机和STM32单片机两部分&#xff1b;建议先学习51单片机&#xff0c;其是STM32等高级单片机的基础&#xff1b;这样再学习STM32时才能融会贯通。 ☀️ 专栏适用人群 &#xff1a;适用于想要…

Solon 框架,单月下载量突破100万了!!!

Solon 框架&#xff0c;单月下载量突破100万了。感谢开源的力量和社区的支持&#xff0c;我们同喜同荣&#xff01;&#xff01;&#xff01;目前&#xff0c;作为新成员加入了“可信开源共同体”&#xff0c;也积极参与中科院的“开源之夏”计划&#xff0c;也备受某军工服务商…

【C++入门】你知道C++为什么会有内联函数吗?

&#x1f466;个人主页&#xff1a;Weraphael ✍&#x1f3fb;作者简介&#xff1a;目前学习C和算法 ✈️专栏&#xff1a;C航路 &#x1f40b; 希望大家多多支持&#xff0c;咱一起进步&#xff01;&#x1f601; 如果文章对你有帮助的话 欢迎 评论&#x1f4ac; 点赞&#x1…

深度优先遍历和广度优先遍历

深度优先遍历和广度优先遍历 文章目录 深度优先遍历和广度优先遍历图图的实现遍历方式深度优先遍历基本思想广度优先遍历基本思想 完整代码 图 介绍&#xff1a;用于表示多对多的关系 图是一种数据结构&#xff0c;其中结点可以具有零个或多个相邻元素。两个结点之间的连接称…

FFmpeg AAC 解码 PCM

1. 概要与流程图 1.1 AAC 转 PCM,需要解码库来实现,目前了解有三种方式,当前使用的是 FFmpeg 库解码 1) faad 解码库网址:faadhttps://sourceforge.net/projects/faac/files/faad2-src/ 2) fdk-aac 编解码库网址: fdk-aac

接口测试入门:轻松掌握 ,让你的后端“心甘情愿”地配合你

目录 前言 简介 什么是API测试&#xff1f; API测试的必要性 API测试的类型 优势 API测试的挑战 【自动化测试工程师学习路线】 前言 在这篇文章中&#xff0c;我将带你轻松入门接口测试并掌握实用技巧&#xff0c;让你能够与后端开发人员更加顺畅地合作。相信我&…

【量化分析】用mplfinance显示交易图时,处理 Expect data.index as DatetimeIndex?

目录 一、说明 二、程序代码和出错描述 三、合理化建议 3.1 读入数据时指定索引 3.2 读入数据后&#xff0c;使用数据前指定日期&#xff08;时间戳&#xff09;索引 一、说明 我打算从比特币数据中获取烛台图。这是我在加载 csv 文件后选择我想要的数据框的代码。然而&…

【2】Kaggle:狗的种类识别,基于论文中的 VGG(11)/ResNet(18)/SENet 结构实现

1.数据集格式 在前面【1】中已经对数据集进行过了处理,在自己实现这一模块,训练数据集的统一格式为下图: 2.VGG11结构 根据论文Karen Simonyan, Andrew Zisserman. Very deep convolutional networks for large-scale image recognition. ICLR, 2015. (VGG) 中的结构,根…