list类
1.熟悉list接口以及使用
list的C++官方文档
1.1list的构造
| 构造函数 constructor | 接口说明 | 
|---|---|
| list (size_type n, const value_type& val = value_type()) | 构造的list中包含n个值为val的元素 | 
| list() | 构造空的list | 
| list (const list& x) | 拷贝构造函数 | 
| list (InputIterator first, InputIterator last) | 用[first, last)区间中的元素构造list(都是左闭右开) | 
代码演示:
//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;
}
1.2list iterator的使用
| 函数声明 | 接口说明 | 
|---|---|
| begin +end | 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器 | 
| rbegin+rend | 返回最后一个元素的reverse_iterator,即end位置,返回第一个元素位置的reverse_iterator,即begin位置 | 
注意:
- begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
- rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
代码演示:
//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(int));
	//使用正向迭代器正向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;
}
1.3list capacity
| 函数声明 | 接口说明 | 
|---|---|
| empty | 检测list是否为空,是返回true,否则返回false | 
| size | 返回list中有效节点的个数 | 
1.4list element access
| 函数声明 | 接口说明 | 
|---|---|
| front | 返回list的第一个节点中值的引用 | 
| back | 返回list的最后一个节点中值的引用 | 
1.5list modifiers
| 函数声明 | 接口说明 | 
|---|---|
| push_front | 在list首元素前插入值为val的元素 | 
| pop_front | 删除list中第一个元素 | 
| push_back | 在list尾部插入值为val的元素 | 
| pop_back | 删除list中最后一个元素 | 
| insert | 在list position 位置中插入值为val的元素 | 
| erase | 删除list position位置的元素 | 
| swap | 交换两个list中的元素 | 
| clear | 清空list中的有效元素 | 
代码演示:
//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;
}
1.6list的迭代器失效
迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
void TestListIterator1()
{
 	int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
 	list<int> l(array, array+sizeof(array)/sizeof(array[0]));
 	auto it = l.begin();
 	while (it != l.end())
 	{
 		// erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值
 		l.erase(it); 
 		++it;
 	}
}
// 改正
void TestListIterator()
{
 	int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
 	list<int> l(array, array+sizeof(array)/sizeof(array[0]));
 	auto it = l.begin();
 	while (it != l.end())
 	{
 		l.erase(it++); // it = l.erase(it);
 	}
}
2.list的模拟实现
template<class T>
struct list_node
{
	list_node(const T& x = T())
		:_date(x)
		,_next(nullptr)
		,_prev(nullptr)
	{}
public:
	T _date;
	list_node<T>* _next;
	list_node<T>* _prev;
};
//T T& T*
//T const T& const T*
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)
	{}
	self& operator++()
	{
		_node = _node->_next;
		return *this;
	}
	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;
	}
	Ref operator* ()
	{
		return _node->_date;
	}
	Ptr operator->()
	{
		return &(_node->_date);
	}
	bool operator!=(const self& s)
	{
		return _node != s._node;
	}
	bool operator==(const self& s)
	{
		return _node == s._node;
	}
public:
};
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;
	const_iterator begin() const
	{
		return const_iterator(_head->_next);
	}
	const_iterator end() const
	{
		return const_iterator(_head);
	}
	iterator begin()
	{
		return iterator(_head->_next);
	}
	iterator end()
	{
		return iterator(_head);
	}
	void empty_init()
	{
		_head = new Node;
		_size = 0;
		_head->_next=_head;
		_head->_prev = _head;
	}
	list()
	{
		empty_init();
	}
	void swap(list<T>& lt)
	{
		std::swap(_head, lt._head);
		std::swap(_size, lt._size);
	}
	//lt2(lt1)
	list(const list<T>& lt)
	{
		empty_init();
		for (auto e : lt)
		{
			push_back(e);
		}
	}
	//lt2=lt1
	list<T>& operator=(list<T> lt)
	{
		swap(lt);
		return *this;
	}
	~list()
	{
		clear();
		delete _head;
		_head = nullptr;
		_size = 0;
	}
	void clear()
	{
		iterator it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}
	void push_back(const T& x)
	{
		insert(end(), x);
	}
	void push_front(const T& x)
	{
		insert(begin(), x);
	}
	void pop_front()
	{
		erase(begin());
	}
	void pop_back()
	{
		erase(--end());
	}
	iterator insert(iterator pos, const T& x)
	{
		Node* cur = pos._node;
		Node* newnode = new Node(x);
		Node* prev = cur->_prev;
		prev->_next = newnode;
		newnode->_next = cur;
		cur->_prev = newnode;
		newnode->_prev = prev;
		++_size;
		return iterator(newnode);
	}
	iterator erase(iterator pos)
	{
		Node* cur = pos._node;
		Node* next = cur->_next;
		Node* prev = cur->_prev;
		delete cur;
		next->_prev = prev;
		prev->_next = next;
		--_size;
		return iterator(next);
	}
	size_t size()
	{
		return _size;
	}
private:
	Node* _head;
	size_t _size;
};










