C++STl——list

发布于:2025-05-11 ⋅ 阅读:(13) ⋅ 点赞:(0)

list的细节

list的细节之一就是其迭代器没有使用指针了,因为指针是无法达到迭代器的效果的,再补充下迭代器的类型,迭代器分为单向,双向,随机迭代器,单向迭代器的容器有forward_list,unordered_map等,单向迭代器只支持++,双向迭代器有list,map,双向迭代器支持++与–,随机迭代器有string,vector,deque支持++,–,+,-,迭代器的名字决定了其类型,list本身是有sort函数的,但是效率过低,不如将他粘贴到vector进行sort在粘贴回来,后续有代码证明。list也存在迭代器失效问题,list的insert相比于vector是没有失效问题的,因为list的insert不存在扩容问题,基本所有的erase都有迭代器失效问题,所以每个erase都会返回删除下一个位置的迭代器,string也是有的,只是string更习惯用[]而不是迭代器进行访问。

头文件

#pragma once

#include<assert.h>

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

		list_node(const T& x = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{}
	};
	//这里是封装了iterator类,因为list的原生指针无法作为迭代器
	// typedef list_iterator<T, T&, T*> iterator;三个模版参数,是为了后面的const做铺垫
	// 如果需要const迭代器,我们是没办法完成的,所以这里封装一个const_iterator,由于普通ite与const的区别
	// 就只有*上,所以就通过添加参数来进行合并
	// typedef list_iterator<T, const T&, const T*> const_iterator;
	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;
		}

		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self operator++(int)//因为tmp会销毁,所以不能传引用
		{
			Self tmp(*this);
			_node = _node->_next;

			return tmp;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		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>
	struct list_const_iterator
	{
		typedef list_node<T> Node;
		typedef list_const_iterator<T> Self;
		Node* _node;

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

		const T& operator*()
		{
			return _node->_data;
		}

		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

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

			return tmp;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		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;
		//typedef list_const_iterator<T> const_iterator;
		typedef list_iterator<T, const T&, const T*> const_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);
		}
		//这里单独封装一个初始化函数不选择用初始化列表进行初始化,是因为后续需要额外创建链表时需要初始化
		//初始化头节点需要创建Node对象并设置其_next和_prev指针指向自身。
		//这类多步操作无法直接在构造函数的初始化列表中完成(初始化列表仅支持表达式,不支持语句)
		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;

			_size = 0;
		}

		list()
		{
			empty_init();
		}

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

		// lt2(lt1)
		//list(const list<T>& lt)
		list(list<T>& lt)
		{
			empty_init();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}

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

		// 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);
			}
		}

		size_t size()
		{
			return _size;
		}

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

			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;*/
			insert(end(), x);
		}

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

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

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

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

			++_size;
		}

		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;

			--_size;

			//return iterator(next);
			return next;
		}
	private:
		Node* _head;
		size_t _size;
	};

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

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

		lt.push_front(1);
		lt.push_front(2);
		lt.push_front(3);
		lt.push_front(4);

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

		lt.pop_back();
		lt.pop_back();
		lt.pop_front();
		lt.pop_front();

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

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

		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			if (*it % 2 == 0)
			{
				it = lt.erase(it);
			}
			else
			{
				++it;
			}
		}

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

	// const T* p1;
	// T* const p2;
	void print(const list<int>& lt)
	{
		// const迭代器的要求不是本身不能修改,而是指向的内容不能修改
		//const list<int>::iterator it = lt.begin();
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			// *it = 10;
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	void test_list3()
	{
		list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);

		for (auto e : lt1)
		{
			cout << e << " ";
		}
		cout << endl;

		list<int> lt2(lt1);

		for (auto e : lt2)
		{
			cout << e << " ";
		}
		cout << endl;

		list<int> lt3 = { 1,2,3,4,5,6 };
		lt1 = lt3;

		for (auto e : lt1)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	struct AA
	{
		int _a1;
		int _a2;

		AA(int a1 = 1, int a2 = 1)
			:_a1(a1)
			, _a2(a2)
		{}
	};

	void test_list4()
	{
		//AA aa1 = { 1,1 };
		vector<AA> v1;
		v1.push_back({ 1,1 });
		v1.push_back({ 2,2 });
		v1.push_back({ 3,3 });
		vector<AA>::iterator it1 = v1.begin();
		while (it1 != v1.end())
		{
			cout << it1->_a1 << ":" << it1->_a2 << endl;
			++it1;
		}
		cout << endl;

		list<AA> lt1;
		lt1.push_back({ 1,1 });
		lt1.push_back({ 2,2 });
		lt1.push_back({ 3,3 });
		list<AA>::iterator lit1 = lt1.begin();
		while (lit1 != lt1.end())
		{
			//cout << (*lit1)._a1 <<":"<< (*lit1)._a2 << endl;
			// 特殊处理,省略了一个->,为了可读性
			cout << lit1->_a1 << ":" << lit1->_a2 << endl;
			cout << lit1.operator->()->_a1 << ":" << lit1.operator->()->_a2 << endl;

			++lit1;
		}
		cout << endl;
	}
}


源码

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<list>
#include<vector>
#include<algorithm>

using namespace std;

//int main()
//{
//	// 21:05
//	list<int> lt1 = {1,2,2,2,3,3,4,2,3,5,6};
//	for (auto e : lt1)
//	{
//		cout << e << " ";
//	}
//	cout << endl;
//	sort(lt1.begin(), lt1.end());
//
//	/*lt1.sort();
//	lt1.unique();*/
//	lt1.remove(2);
//	for (auto e : lt1)
//	{
//		cout << e << " ";
//	}
//	cout << endl;
//
//	return 0;
//}

void test_op1()
{
	srand(time(0));
	const int N = 1000000;

	list<int> lt1;
	vector<int> v;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand() + i;
		lt1.push_back(e);
		v.push_back(e);
	}

	int begin1 = clock();
	// 排序
	sort(v.begin(), v.end());
	int end1 = clock();

	int begin2 = clock();
	lt1.sort();
	int end2 = clock();

	printf("vector sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

void test_op2()
{
	srand(time(0));
	const int N = 1000000;

	list<int> lt1;
	list<int> lt2;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand() + i;
		lt1.push_back(e);
		lt2.push_back(e);
	}

	int begin1 = clock();
	// 拷贝vector
	vector<int> v(lt2.begin(), lt2.end());
	// 排序
	sort(v.begin(), v.end());

	// 拷贝回lt2
	lt2.assign(v.begin(), v.end());

	int end1 = clock();

	int begin2 = clock();
	lt1.sort();
	int end2 = clock();

	printf("list copy vector sort copy list sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

void test_list1()
{
	// 一个链表的节点移动到另一个链表
	std::list<int> lt1 = { 1, 2, 3, 4, 5 };
	std::list<int> lt2 = { 10, 20, 30, 40 };
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;

	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl;

	lt1.splice(lt1.begin(), lt2);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;

	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl;

	// 一个链表中节点调整顺序
	std::list<int> lt3 = { 1, 2, 3, 4, 5 };
	int x;
	cin >> x;
	auto it = find(lt3.begin(), lt3.end(), x);
	if (it != lt3.end())
	{
		lt3.splice(lt3.begin(), lt3, it);
	}

	for (auto e : lt3)
	{
		cout << e << " ";
	}
	cout << endl;
}

#include"List.h"

int main()
{
	prime::test_list4();

	return 0;
}