list的介绍及使用(构造, 迭代器, 使用, 迭代器失效的处理办法)

list的介绍及使用

1、list是可以在常数范围类任意位置进行插入和删除的序列式容器,可以前后双向迭代
2、list的底层是双向带头循环链表
3、list个forword_list类似,最大的不同是forword只能向前迭代。
4、list在任意位置插入删除的效率较高
5、list不支持随机访问

1、list类的构造

构造函数( (constructor)) 接口说明
list() 构造空的list
list (size_type n, const value_type& val = value_type()) 构造的list中包含n个值为val的元素
list (const list& x) 拷贝构造函数
list (InputIterator first, InputIterator last) 用[first, last)区间中的元素构造list
#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> l1;//构造空的l1
	list<int> l2(4, 100);//构造4个值为100的list
	list<int> l3(l2.begin(), l2.end());//用l2的begin-end区间构造l3
	list<int> l4(l3);//拷贝构造l4

	//用数组构造list
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	list<int> l5(arr, arr + sizeof(arr) / sizeof(arr[0]));

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

	//用范围for遍历
	for (auto& e : l5)
	{
		cout << e << " ";
	}
	cout << endl;


	system("pause");
	return 0;
}

2、list的迭代器使用

函数声明 接口说明
begin +end 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
rbegin +rend 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的reverse_iterator,即begin位置
#include <iostream>
#include <list>
using namespace std;

void PrintList(const list<int>& l)
{
	for (list<int>::const_iterator cit = l.begin(); cit != l.end(); cit++)
	{
		cout << *cit << " ";
	}
	cout << endl;
	
	for (list<int>::const_reverse_iterator crit = l.rbegin(); crit != l.rend(); crit++)
	{
		cout << *crit << " ";
	}
	cout << endl;
}

int main()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));

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

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

	PrintList(l);
	system("pause");
	return 0;
}

注:
1、begin和end为正向迭代器,对迭代器执行++操作,迭代器向后移动
2、rbegin和rend为反向迭代器,对迭代器执行++操作,迭代器向前移动

3、list类的基本操作

函数声明 接口说明
empty 检测list是否为空,是返回true,否则返回false
size 返回list中有效节点的个数
front 返回list的第一个节点中值的引用
back 返回list的最后一个节点中值的引用
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中的有效元素
#include <iostream>
#include <vector>
#include <list>

using namespace std;

void PrintList(list<int>& l)
{
	for (auto& e : l)
	{
		cout << e << " ";
	}
	cout << endl;
}

void TestList1()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));

	l.push_back(10);
	l.push_back(11);
	l.push_back(12);
	l.push_front(0);
	PrintList(l);

	l.pop_back();
	l.pop_back();
	l.pop_front();
	PrintList(l);
}

void TestList2()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));

	auto pos = l.begin()++;
	cout << *pos << endl;

	l.insert(pos, 4);
	PrintList(l);

	l.insert(pos, 5, 6);
	PrintList(l);

	vector<int> v{ 7, 8, 9 };
	l.insert(pos, v.begin(), v.end());
	PrintList(l);

	l.erase(pos);
	PrintList(l);

	l.erase(l.begin(), l.end());
	PrintList(l);
}

void TestList3()
{
	int arr1[] = { 1, 2, 3, 4, 5 };
	list<int> l1(arr1, arr1 + sizeof(arr1) / sizeof(arr1[0]));
	int arr2[] = { 6, 7, 8, 9, 10 };
	list<int> l2(arr2, arr2 + sizeof(arr2) / sizeof(arr2[0]));
	
	PrintList(l1);
	PrintList(l2);

	l1.swap(l2);
	PrintList(l1);
	PrintList(l2);

	l2.clear();
	cout << l2.size() << endl;
}

int main()
{
	TestList1();
	TestList2();
	TestList3();

	system("pause");
	return 0;
}

4、list类的迭代器失效问题

list迭代器失效指的是,迭代器所指向的节点无效,即该节点被删除了,list底层结构为双向带头循环链表,因此在list进行插入操作不会导致迭代器失效, 只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响

#include <iostream>
#include <list>

using namespace std;

void TestListIterator1()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));

	list<int>::iterator it = l.begin();
	for (; it != l.end(); it++)
	{
		//erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值
		l.erase(it);
	}
}

void TestListIterator2()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));

	list<int>::iterator it = l.begin();
	for (; it != l.end();)
	{
		//erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值
		l.erase(it++);
	}
}


int main()
{
	//TestListIterator1();
	TestListIterator2();
	system("pause");
	return 0;
}

发布了117 篇原创文章 · 获赞 48 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/gp1330782530/article/details/105391012