【c++】list的介绍及使用

以下是阿鲤对list容器的总结希望对大家有所帮助,若有误请慷慨指出

1:list的介绍

2:list的使用


1:list的介绍

1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向
其前一个元素和后一个元素。
3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高
效。
4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率
更好。
5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list
的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间
开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这
可能是一个重要的因素

2:list的使用

以下是阿鲤整理的常用接口,希望对大家有用。

 

2.1:list的构造函数

构造函数接口说明
llist()构造空的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)区间中的元素构造li

eg:

#include<iostream>
#include<list>

using namespace std;

int main()
{
	int arr[] = { 1,2,3,4,5,6 };
	list<int> l1;//空构造
	list<int> l2(3, 8);//3个8
	list<int> l3(arr + 2, arr + 5);//一段(数组)
	list<int> l4(l3.begin(), l3.end());//一段(list迭代器)
	list<int> l5(l3);//拷贝构造
	return 0;
}

 

 

2.2:list iterator的使用

函数声明接口说明
begin()返回第一个元素的迭代器
end()返回最后一个元素下一个位置的迭代器
rbegin()返回第一个元素的reverse_iterator(end)的位置
rend()返回最后一个元素下一个位置的reverse_iterator(begin)的位置
cbegin()(C++11) 返回第一个元素的cosnt_iterator
cend() (C++11) 返回最后一个元素下一个位置的const_iterator
crbegin()(C++11)即crend()位置
crend() (C++11) 即crbegin()位置

eg: 

#include<iostream>
#include<list>

using namespace std;

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8 };
	list<int> l1(arr,arr+8);
	list<int>::iterator it;
	for (it = l1.begin(); it != l1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	list<int>::reverse_iterator rt;
	for (rt = l1.rbegin(); rt != l1.rend(); rt++)
	{
		cout << *rt << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

 

【注意】

1. begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动

2. rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动

3. cbegin与cend为const的正向迭代器,与begin和end不同的是:该迭代器指向节点中的元素值不能修改

4. crbegin与crend为const的反向迭代器,与rbegin和rend不同的是:该迭代器指向节点中的元素值不能修改

 

2.3:list capacity

函数声明接口说明
bool empty() const检测链表是否为空
size_t size() const 查看链表中有效节点个数

eg:

#include<iostream>
#include<list>

using namespace std;

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8 };
	list<int> l1(arr,arr+8);
	cout << l1.empty() << endl;
	cout << l1.size() << endl;
	system("pause");
	return 0;
}

 

2.4:list element access

函数声明接口说明
reference front()返回list中第一个节点的引用
const_reference front()返回list中第一个节点中的const引用
reference back()返回list中最后一个节点的引用
const reference back()返回list中最后一个节点的const引用

eg:

#include<iostream>
#include<list>

using namespace std;

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8 };
	list<int> l1(arr,arr+8);
	cout << l1.front()++ << endl;//因为是引用,所以可以修改
	cout << l1.front() << endl;
	cout << l1.back() << endl;
	system("pause");
	return 0;
}

 

2.5: list modifie

eg:

#include<iostream>
#include<list>

using namespace std;

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8 };
	list<int> l1(arr,arr+8);
	for (auto &i : l1)
	{
		cout << i << " ";
	}
	cout << endl;
	l1.push_back(666);// 插入
	for (auto &i : l1)
	{
		cout << i << " ";
	}
	cout << endl;
	l1.pop_front();//删除
	for (auto &i : l1)
	{
		cout << i << " ";
	}
	cout << endl;
	list<int>::iterator pos = ++l1.begin();
	pos++;//list中的iterator只能++和--,不能一下加多个。 
	l1.insert(pos, 88);
	for (auto &i : l1)
	{
		cout << i << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

 

2.6:迭代器失效

前面说过,此处大家可将迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值