c++list

1 篇文章 0 订阅
  • list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
  • list的底层是双向链表结构,双向链表中每个元素存储在互不关联的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
  • list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
  • 与其他序列式容器相比(array,vector,deque),list通常在任意位置进行插入,移除元素的执行效率更好。
  • 与其他序列容器相比,list和forward_list最大缺陷是不支持任意位置的随机访问,比如:要访问list的第六个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销:list还需要一些额外的空间,以保存每个节点的相关联信息
  1. list的构造
#include<iostream>
#include<list>
using namespace std;
int main()
{
	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);

	//以数组为迭代器区间构造l5
	int arr[] = {16,2,77,29};
	list<int> l5(arr,arr+sizeof(arr)/sizeof(int));
	

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

	//c++11迭代器打印

	for (auto& x : l5)
	{
		cout << x << " ";
	}
	cout << endl;
	return 0;
}
  1. list iterator的使用
#include<iostream>
#include<list>
using namespace std;
int main()
{
	int arr[] = {1,2,3,4,5,6,7,8,9,0};
	//使用数组作为迭代器进行构造
	list<int> l(arr, arr +sizeof(arr)/sizeof(int) );
	//使用正向迭代器正向打印list中的元素
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	//使用反向迭代器逆向打印list中的元素
	for (list<int>::reverse_iterator it = l.rbegin(); it != l.rend();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	//const的正向迭代器
	auto cit = l.cbegin();
	cout << typeid(cit).name() << endl;
	for (auto &xx : l)
	{
		cout << xx << " ";
	}
	cout << endl;
	
	return 0;
}

此处先将迭代器理解成一个指针,该指针指向list中的某个节点

  • begin与end为正向迭代器,对迭代器执行++操作,迭代器向前移动
  • rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
  • cbegin与cend为const的正向迭代器,与begin和end不同的是:该迭代器指向的节点的值不能被修改
  • crbegin与crend为const的反向迭代器,与rbegin和rend不同的是:该迭代器指向节点的元素值不能被修改
  1. list capacity
#include<iostream>
#include<list>
using namespace std;
int main()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	//用数组当作迭代器进行构造
	list<int> l(arr, arr + sizeof(arr) / sizeof(int));
	//打印list中有效节点的个数
	cout << l.size() << endl;
	//检查list是否为空
	if (l.empty())
	{
		cout << "空的list" << endl;
	}
	else
	{
		for (auto &e : l)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	return 0;
}
  1. list element access
#include<iostream>
#include<list>
using namespace std;
int main()
{
	int arr[] = {1,2,3,4,5,6,7,8,9,0};
	list<int> l1(arr, arr + sizeof(arr) / sizeof(int));
	for (auto& e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	//将list中的第一个节点与最后一个节点中的值改为10
	l1.front() = 10; //list的第一个节点
	l1.back() = 10;//list的第二个节点
	for (auto& e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	const list<int> l2(arr, arr + sizeof(arr) / sizeof(arr[0]));
	const int& ca = l2.front();

	return 0;
}
  1. list modifiers
#include<iostream>
#include<list>
using namespace std;
void PrintList(list<int>& l)
{
	for (auto &x : l)
	{
		cout << x << " ";
	}
	cout << endl;
}
void TestList1()
{
	int arr[] = { 1, 2, 3 };
	list<int> L(arr, arr + sizeof(arr) / sizeof(arr[0]));

	//在list尾部插入4,头部插入0
	L.push_back(4);
	L.push_front(0);
	PrintList(L);

	//删除list尾部节点和头部节点
	L.pop_back();
	L.pop_front();
	PrintList(L);
}
class Date{
public:
	Date(int year=1900,int month=1,int day=1)
		:_year(year)
		, _month(month)
		, _day(_day)
	{
		cout << "Date(int,int,int)" << this << endl;
	}
	Date(const Date& d)
		:_year(d._year)
		, _month(d._month)
		, _day(d._day)
	{
		cout << "Date(const Date&)" << this << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
//push_back尾插:先构造好元素,然后将元素拷贝到节点中,插入时先调构造函数,在调拷贝构造函数
//emplace_back尾插:先构造节点,然后调用构造节点,然后调用构造函数在节点中直接构造对象
//emplace_back比push_back更高效,少一次拷贝构造函数的调用
void TestList2()
{
	list<Date> l;
	Date d(2018, 10, 21);
	l.emplace_back(2018, 10, 21);
	l.emplace_front(2018, 10, 19);
}

void TestList3()
{
	int arr[] = {1,2,3};
	list<int> L(arr, arr + sizeof(arr) / sizeof(arr[0]));
	//获取链表中第二个节点
	auto pos = ++L.begin();
	cout << *pos << endl;
	//在pos前插入值为4的元素
	L.insert(pos, 4);
	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);
}
void TestList4()
{
	//用数组来构造list
	int arr[] = { 1, 2, 3 };
	list<int> l1(arr,arr+sizeof(arr)/sizeof(arr[0]));
	PrintList(l1);


	//将l1中元素个数增加到10个,多出元素用默认值填充
	l1.resize(10);
	PrintList(l1);

	//将l1中的元素增加到20个,多出的元素用4来填充
	l1.resize(20, 4);
	PrintList(l1);

	//将l1中的元素减少到5个
	l1.resize(5);
	PrintList(l1);

	//用vector中的元素来构造list
	vector<int> v{ 4, 5, 6 };
	list<int> l2(v.begin(), v.end());
	PrintList(l2);

	//交换l1和l2中的元素
	l1.swap(l2);
	PrintList(l1);
	PrintList(l2);

	//将l2中的元素清空
	l2.clear();
	cout << l2.size() << endl;
}
int main(){
	//TestList2();
	TestList4();
	return 0;
}
  1. list的迭代器失效
    迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头节点的双向循环链表,因此在list中进程插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除的节点的迭代器,其他迭代器不会受到影响。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值