C++中list的用法

list

list的底层结构
list 是可以随意插入和删除的序列式容器,list底层结构是带头结点双向循环链表;
list中常用接口说明
1.list的构造
list():构造空的list
list(size_t n,const T& data = T()):构造的list中包含n个值为data的元素;
list(first,last):用[first, last)区间中的元素构造list;
list(const list& x):拷贝构造函数;

#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); // 用l3拷贝构造l4
	
	// 以数组为迭代器区间构造l5
	int array[] = { 16, 2, 77, 29 };
	list<int> l5(array, array + sizeof(array) / sizeof(int));
	
	// 用迭代器方式打印l5中的元素
	for (list<int>::iterator it = l5.begin(); it != l5.end(); it++)
		cout << *it << " ";
	cout << endl;

	// C++11范围for的方式遍历
	for (auto& e : l5)
		cout << e << " ";
	cout << endl;
	return 0;
}

2.迭代器的使用
begin()/end():返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器;
rbegin()/rend():返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的 reverse_iterator,即begin位置;

#include<iostream>
#include<list>

using namespace std;

void print_list(const list<int>& l)
{
	// 注意这里调用的是list的 begin() const,返回list的const_iterator对象
	for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
	{
		cout << *it << " ";
	}
	cout << endl;
}

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

	// 使用正向迭代器正向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;
	return 0;
}

3.容量操作
size():返回list中有效节点的个数;
empty():检测list是否为空,是返回true,否则返回false;
4.元素访问操作
front():返回list的第一个节点中值的引用;
back():返回list的最后一个节点中值的引用;
注:如果要访问任意问题的元素:find(first,last,data);若有重复的,它只能查询到第一个;
5.元素修改
push_back():在list尾部插入元素;
pop_back():删除list中第一个元素;
push_front():在list首元素前插入元素;
pop_front():删除list中第一个元素;
insert():在list中插入元素;
erase():删除list中的元素;
clear():清空list中的有效元素;
swap():交换两个list中的元素;

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

//push_back/pop_back/push_front/pop_front
void TestList1()
{
	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 TestList3()
{
	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 TestList4()
{
	// 用数组来构造list
	int array1[] = { 1, 2, 3 };
	list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
	PrintList(l1);
	// 交换l1和l2中的元素
	l1.swap(l2);
	PrintList(l1);
	PrintList(l2);
	// 将l2中的元素清空
	l2.clear();
	cout << l2.size() << endl;
}

vector和list的区别

vectorlist
底层结构动态顺序表,一段连续空间带头结点的双向循环列表
随机访问支持随机访问,访问某个元素效率O(1)不支持随机访问,访问某个元素效率O(N)
插入和删除任意位置插入和删除效率低,需要搬移元素,时间复杂度为O(N),插入时有可能需要增容,增容:开辟新空间,拷贝元素,释放旧空间,导致效率更低任意位置插入和删除效率高,不需要搬移元素,时间复杂度为O(1)
空间利用率底层为连续空间,不容易造成内存碎片,空间利用率高,缓存利用率高底层节点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低
迭代器原生态指针对原生态指针(节点指针)进行封装
迭代器失效在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效,删除时,当前迭代器需要重新赋值否则会失效插入元素不会导致迭代器失效,删除元素时,只会导致当前迭代器失效,其他迭代器不受影响
使用场景需要高效存储,支持随机访问,不关心插入删除效率大量插入和删除操作,不关心随机访问
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用:在C++list表示双向链表,它是一种线性容器,可以在任意位置插入和删除元素。可以利用正向迭代器遍历list容器进行操作。例如,使用list<int>迭代器it来遍历容器lt,可以使用lt.begin()获取list的第一个元素的迭代器,使用lt.end()获取list的末尾元素的下一个位置的迭代器。然后使用while循环和迭代器自增来遍历并输出list的元素。 引用展示了C++使用list的一些常见操作。比如,使用push_back()方法在list的末尾插入元素,使用merge()方法将两个list合并并按照指定的排序方式进行排序。另外,可以使用begin()和end()方法获取list的起始和结束迭代器,并使用for循环和迭代器自增来遍历并输出list的元素。 引用展示了C++list进行删除操作的用法。可以使用erase()方法来删除list的元素,其可以指定要删除的元素的位置或者范围。删除元素后,list的迭代器不会失效,仍然可以使用它们来继续遍历和操作list。 因此,C++list可以通过迭代器来遍历并操作其的元素,可以进行插入、删除和合并等操作。同时,使用迭代器遍历和操作list时需要注意迭代器的有效性,确保在删除元素后仍然能够正确使用迭代器。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【C++list使用](https://blog.csdn.net/chuxinchangcun/article/details/128571927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [C++list用法详解](https://blog.csdn.net/fengruoying93/article/details/108222992)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值