list(C++)

1.1 list的介绍

1.2 list的使用

list中的接口比较多,此处类似,只需要掌握如何正确的使用,然后再去深入研究背后的原理,已 达到可扩展的能力。以下为list中一些常见的重要接口。

1.2.1 list的构造

1.2.2 list iterator的使用

此处,大家可暂时将迭代器理解成一个指针,该指针指向list中的某个节点。

 

 【注意】

1. begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动

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

1.2.3 list capacity

 1.2.4 list element access

 1.2.5 list modifiers

1.2.6 list的迭代器失效

前面说过,此处大家可将迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的节点的无 效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入 时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭 代器,其他迭代器不会受到影响。

2. list的模拟实现

#include<assert.h>
#include"Test.cpp"

namespace bit
{
	template<class T>
	struct list_node
	{
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;

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

	template<class T,class Ref,class ptr>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T, Ref, ptr> Self;
		Node* _node;

		list_iterator(Node* _node)
			:_node(node)
		{}

		Ref operator*()
		{
			return _node->_data;
		}

		ptr operator->()
		{
			return &_node->_data;
		}

		//返回的是next
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		//返回的是prev
		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator++(int)
		{
			Self tmp(*this);
			_node = _node->_next;

			return tmp;
		}

		Self operator--(int)
		{
			Self tmp(*this);
			_node = _node->_prev;

			return tmp;
		}

		bool operator!=(const Self& s) const
		{
			return _node != s._node;
		}

		bool operator==(const Self& s) const
		{
			return _node == s._node;
		}
	};

	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:

		typedef list_iterator<T, T&, T*> iterator;
		tyedef list_iterator<T, const T&, const T*> _List_const_iterator;

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

		iterator end()
		{
			return _head;
		}

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

		const_iterator end() const
		{
			return _head->_prev;
		}

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}

		list()
		{
			empty_init();
		}

		list(initalizer_list<T> il)
		{
			empty_init();
			for (anto& e : il)
			{
				push_back(e);
			}
		}

		list(const list<T>& lt)
		{
			empty_init();

			for (anto& e : lt)
			{
				push_back(e);
			}
		}

        // lt1 = lt3
		list<T>& operator=(list<T> lt)
		{
			swap(lt);

			return *this;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		void clear()
		{
			auto it = begin();
			while (it != end)
			{
				it = erase(it);
			}
		}

		void swap(list<T> lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}

		void push_back(const T& x)
		{
			insert(end(), x);
		}

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

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

			++_size;

			return newnode;
		}

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

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

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

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

			prev->_next = next;
			next->_prev = prev;
			delete pos._node;

			--size;

			return next;
		}

		size_t size() const
		{
			return _size;
		}

		bool empty() const
		{
			return _size ==0;
		}

		private:
			Node* _head;
			size_t _size;
	};

	struct AA
	{
		int _a1 = 1;
		int _a2 = 1;
	};

	template<class Container>
	void print_container(const Container& con)
	{
		// const iterator -> 迭代器本身不能修改
		// const_iterator -> 指向内容不能修改
		typename Container::const_iteratro it = con.begin();
		while (it != con.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}
}

3. list与vector的对比

vector与list都是STL中非常重要的序列式容器,由于两个容器的底层结构不同,导致其特性以及 应用场景不同,其主要不同如下:

vectorlist
底 层 结 构动态顺序表,一段连续空间带头结点的双向循环链表
随 机 访 问支持随机访问,访问某个元素效率O(1)不支持随机访问,访问某个元 素效率O(N)
插 入 和 删 除任意位置插入和删除效率低,需要搬移元素,时间 复杂度为O(N),插入时有可能需要增容,增容: 开辟新空间,拷贝元素,释放旧空间,导致效率更 低任意位置插入和删除效率高, 不需要搬移元素,时间复杂度 为O(1)
空 间 利 用 率底层为连续空间,不容易造成内存碎片,空间利用 率高,缓存利用率高底层节点动态开辟,小节点容 易造成内存碎片,空间利用率 低,缓存利用率低
迭 代 器原生态指针对原生态指针(节点指针)进行 封装
迭 代 器 失 效在插入元素时,要给所有的迭代器重新赋值,因为 插入元素有可能会导致重新扩容,致使原来迭代器 失效,删除时,当前迭代器需要重新赋值否则会失 效插入元素不会导致迭代器失 效,删除元素时,只会导致当 前迭代器失效,其他迭代器不 受影响
使 用 场 景需要高效存储,支持随机访问,不关心插入删除效 率大量插入和删除操作,不关心 随机访问

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL(Standard Template Library)是C++标准库中的一个重要组成部分,提供了一系列的模板类和函数,用于实现常用的数据结构和算法。其中包括vector、list等容器类。 引用\[1\]中的代码展示了如何使用list容器,包括插入元素、删除元素、判断容器是否为空、获取容器大小等操作。通过list容器的push_back、push_front、pop_back等成员函数可以实现元素的插入和删除。同时,可以使用迭代器进行遍历和访问容器中的元素。 引用\[2\]中的代码展示了如何遍历和打印一个string类型的vector容器中的元素。通过迭代器的方式,可以依次访问vector容器中的每个元素,并进行相应的操作。 引用\[3\]中的代码展示了如何遍历和打印一个int类型的list容器中的元素。同样地,通过迭代器的方式,可以依次访问list容器中的每个元素,并进行相应的操作。 综上所述,C++ STL提供了丰富的容器类和算法,可以方便地进行数据结构和算法的实现。在实际编程中,可以根据需求选择合适的容器类,并使用相应的成员函数和迭代器进行操作和遍历。 #### 引用[.reference_title] - *1* [⭐️STL⭐️之list,set,map全解,❤️算法必备❤️下>](https://blog.csdn.net/weixin_45920495/article/details/120256992)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v12^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【C++】500行代码全解STL_string_vector_remove和erase实现逻辑_deque_queue_list_pair_set_multiset_map_...](https://blog.csdn.net/qq_31295885/article/details/104788427)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v12^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值