【C++】一覧


説明する

1.listは一定範囲内の任意の位置に挿入・削除が可能な連続コンテナであり、双方向に反復可能です。

2. リストの最下層は二重連結リスト構造になっており、二重連結リストの各要素は互いに関連性のない独立したノードに格納され、ノード内では前の要素と次の要素を指すポインタが配置されます。 。

3. list は forward_list と非常に似ています。主な違いは、forward_list が単一リンクされたリストであり、一方向にのみ反復できることです。

4. 他のシリアルコンテナ(配列、ベクトル、デキュー)と比較して、任意の位置の要素を挿入および削除するリストの実行効率が優れています。

5. 他の順次コンテナと比較すると、list と forward_list の最大の欠点は、任意の位置でのランダム アクセスをサポートしていないことです。たとえば、リストの 6 番目の要素にアクセスするには、既知の位置 (たとえば、頭または尾)をその場所に移動します。

6. リストには、各ノードの関連情報を保存するための追加スペースも必要です。

1.リスト(STLライブラリ)

共通インターフェース

ここに画像の説明を挿入

リストの用途

リンクリストヘッドプラグ、リンクリストテールプラグ
ここに画像の説明を挿入

リンクリストの先頭を削除、リンクリストの末尾を削除
ここに画像の説明を挿入

リンクリストのトラバーサル
ここに画像の説明を挿入

リンクリストの指定位置への挿入・削除
ここに画像の説明を挿入

クリアクリア
ここに画像の説明を挿入

コンストラクタ
ここに画像の説明を挿入

2、リストシミュレーションの実装

1.ノード

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

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

2. コンストラクター、デストラクター、代入演算子のオーバーロード、clear 関数

void empty_init()
{
    
    
	_head = new node;
	_head->_next = _head;
	_head->_prev = _head;
}
//构造函数
list()
{
    
    
	//初始化内部成员
	empty_init();
}
//迭代器构造函数
template<class Iterator>
list(Iterator first, Iterator last)
{
    
    
	empty_init();
	while (first != last)
	{
    
    
		push_back(*first);
		++first;
	}
}
void swap(list<T>& tmp)
{
    
    
	//std标准库中的swap函数
	std::swap(_head, tmp._head);
}
//拷贝构造
list(const list<T>& lt)
{
    
    
	empty_init();
	list<T> tmp(lt.begin(), lt.end());
	swap(tmp);
}

//不能使用引用,会使原始数据发生改变  现代写法,传参数不使用引用,进行拷贝构造,然后交换
list<T>& operator=(list<T> lt)
{
    
    
	swap(lt);
	return *this;
}
//析构函数
~list()
{
    
    
	clear();
	delete _head;
	_head = nullptr;
	
}
//清除函数,用来清除链表数据,保留头结点
void clear()
{
    
    
	iterator it = begin();
	while (it != end())
	{
    
    
		it = erase(it);
	}
}

3. イテレータ

リストのイテレータはベクトルや文字列とは異なり、ネイティブポインタではなくリンクリストのノードポインタをカプセル化したものであり、ベクトルや文字列と同様の機能を持つイテレータを実現しています。リストの反復子は本質的にリンク リストのノードのポインタであり、リンク リストの構造が特殊であり、格納されているアドレスが連続していないため、リンク リスト ノードのポインタは ++ 演算を実行できません。演算子のオーバーロードにより、イテレータに対して実行できるさまざまな ++ 操作や、比較、逆参照などが動作します。

//迭代器的三个类模板参数,第一个是迭代器指向内容的数据类型,
//第二个参数是为了可以同时实现普通迭代器和const迭代器,使用模板参数,减少写冗余代码
//第三个参数是重载->操作符,为了方便迭代器指向的元素类型是自定义类型,可以使用迭代器访问其成员变量
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* n)
		:_node(n)
	{
    
    }
	//不需要写拷贝构造,因为不需要去析构

	Ref operator*()
	{
    
    
		return _node->_data;
	}
	ptr operator->()
	{
    
    
		//重载的->返回的是指针类型,在使用的时候访问成员变量得使用两个->,编译器进行优化为一个->
		return _node->_data;
	}

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

	self& operator--()
	{
    
    
		_node = _node->_prev;
		return *this;
	}
	self operator--(int)
	{
    
    
		self ret(*this);
		_node = _node->_prev;
		return ret;
	}

	bool operator!=(const self& s)
	{
    
    
		//判断指针
		return _node != s._node;
	}
	bool operator==(const self& s) 
	{
    
    
		//判断指针
		return _node == s._node;
	}
};

3.begin()、end()

begin() は最後の要素を指す反復子を返し、
end() によって返される反復子は最後の要素の後の位置を指し、最後の要素の後の位置はヘッド ノードであるため、end() は最後の要素を指す反復子を返します。ヘッドノード反復子

ここに画像の説明を挿入

typedef _list_iterator<T,T&,T*> iterator;
typedef _list_iterator<T,const T&,const T*> const_iterator;
//这里使用typedef更方便使用实现的迭代器,
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);
}

4. ヘッドプラグ、テールプラグ、指定位置挿入

void push_back(const T& x)
{
    
    
	//可以复用insert    
	//insert(end(), x);
	node* tail = _head->_prev;
	node* new_node = new node(x);

	tail->_next = new_node;
	new_node->_prev = tail;

	new_node->_next = _head;
	_head->_prev = new_node;
}
//头插
void push_front(const T& x)
{
    
    
	//复用insert 头插即为begin()位置插入元素
	insert(begin(), x);
}
//指定位置插入
void insert(iterator pos,const T& x)
{
    
    
	node* cur = pos._node;
	node* prev = cur->_prev;

	node* new_node = new node(x);

	prev->_next = new_node;
	new_node->_prev = prev;
	new_node->_next = cur;
	cur->_prev = new_node;		 
}

5.先頭削除、末尾削除、指定位置削除

//pos位置删除元素
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;

	return iterator(next);
}
//尾删   复用erase
void pop_back()
{
    
    
	erase(--end());
}
//头删   复用erase
void pop_front()
{
    
    
	erase(begin());
}

3. シミュレーション実装ソースコード

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
#include<algorithm>


namespace tzc {
    
    

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

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

	};

	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* n)
			:_node(n)
		{
    
    }
		//不需要写拷贝构造,因为不需要去析构

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

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

		self& operator--()
		{
    
    
			_node = _node->_prev;
			return *this;
		}
		self operator--(int)
		{
    
    
			self ret(*this);
			_node = _node->_prev;
			return ret;
		}

		bool operator!=(const self& s)
		{
    
    
			//判断指针
			return _node != s._node;
		}
		bool operator==(const self& s) 
		{
    
    
			//判断指针
			return _node == s._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;

		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);
		}
		void empty_init()
		{
    
    
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		list()
		{
    
    
			empty_init();
		}
		template<class Iterator>
		list(Iterator first, Iterator last)
		{
    
    
			empty_init();
			while (first != last)
			{
    
    
				push_back(*first);
				++first;
			}
		}
		//拷贝构造
		//list(const list<T>& lt)
		//{
    
    
		//	empty_init();
		//	for (auto e : lt)
		//	{
    
    
		//		push_back(e);
		//	}
		//}
		void swap(list<T>& tmp)
		{
    
    
			//std标准库中的swap函数
			std::swap(_head, tmp._head);
		}
		list(const list<T>& lt)
		{
    
    
			empty_init();
			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}

		//不能使用引用,会使原始数据发生改变
		list<T>& operator=(list<T> lt)
		{
    
    
			swap(lt);
			return *this;
		}

		~list()
		{
    
    
			clear();
			delete _head;
			_head = nullptr;
			
		}
		void clear()
		{
    
    
			iterator it = begin();
			while (it != end())
			{
    
    
				it = erase(it);
			}
		}
		void push_back(const T& x)
		{
    
    
			//可以复用insert    
			//insert(end(), x);
			node* tail = _head->_prev;
			node* new_node = new node(x);

			tail->_next = new_node;
			new_node->_prev = tail;

			new_node->_next = _head;
			_head->_prev = new_node;
		}
		void push_front(const T& x)
		{
    
    
			insert(begin(), x);
		}

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

			node* new_node = new node(x);

			prev->_next = new_node;
			new_node->_prev = prev;
			new_node->_next = cur;
			cur->_prev = new_node;
				 
		}

		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;

			return iterator(next);
		}

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

	private:
		node* _head;
	};

	void print_list(const list<int>& lt)
	{
    
    
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
    
    
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}

	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())
		{
    
    
			cout << *it << " ";
			it++;
		}
		cout << endl;
		
		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);

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

		auto pos = lt.begin();
		++pos;
		lt.insert(pos, 20);
		for (auto e : lt)
		{
    
    
			cout << e << " ";
		}
		cout << endl;


		lt.push_back(5);
		lt.push_front(100);
		lt.pop_back();
		for (auto e : lt)
		{
    
    
			cout << e << " ";
		}
		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;

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

	void test_list4()
	{
    
    
		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;
		lt3 = lt2;
		for (auto e : lt3)
		{
    
    
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list5()
	{
    
    
		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>::iterator it = lt1.begin();
		lt1.insert(it, 20);
		for (auto e : lt1)
		{
    
    
			cout << e << " ";
		}
		cout << endl;

		it = lt1.begin();
		lt1.erase(it);
		for (auto e : lt1)
		{
    
    
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list6()
	{
    
    
		list<int>list1, list2;
		for (int i = 1; i <= 4; i++)
		{
    
     //list1  1 2 3 4
			list1.push_back(i);
		}
		for (int i = 1; i <=3; i++)
		{
    
      //list2  10   20   30
			list2.push_back(i*10);
		}

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

おすすめ

転載: blog.csdn.net/Tianzhenchuan/article/details/131750579