0
点赞
收藏
分享

微信扫一扫

StructLM: Towards Building Generalist Models for Structured Knowledge Grounding

勇敢乌龟 03-03 18:30 阅读 3

1、list的介绍

1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代

2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。

3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,list让其更简单高效。

4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好

5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)

2、list的使用

list中的接口比较多,此处类似,只需要掌握如何正确的使用,然后再去深入研究背后的原理,以达到可扩展 的能力。以下为list中一些常见的重要接口。

        2、1list的构造

void test1()
{
	list<int> l1(10,1);
	for (auto e : l1)
		cout << e << " ";//1 1 1 1 1 1 1 1 1 1
	cout << endl;
	
	list<int> l2;// 空list

	list<int> l3(l1.begin(), l1.end());//迭代器范围构造
	for (auto e : l3)
		cout << e << " ";//1 1 1 1 1 1 1 1 1 1
	cout << endl;

	list<int> l4(l1);//拷贝构造
	for (auto e : l4)
		cout << e << " ";//1 1 1 1 1 1 1 1 1 1
	cout << endl;
}

        2、2list iterator

注意list的最后一个有值结点的下一个位置,并不是list.begin(),而是list.end(),则list.end()就是一个多出来的结点。这里就是为了和其他容器的迭代器保持一致。仍然保持[ begin() , end() ),左闭右开区间。

同样,list的迭代器和其他迭代器是一样的。

void test2()
{
	int arr[] = { 1,2,3,4,5 };
	list<int> l1(arr, arr + sizeof(arr) / sizeof(arr[0]));//迭代器拷贝构造

	list<int>::iterator it1 = l1.begin();
	while (it1 != l1.end())
		cout << *it1++ << " ";//1 2 3 4 5 正向遍历
	cout << endl;

	list<int>::reverse_iterator it2 = l1.rbegin();
	while (it2 != l1.rend())
		cout << *it2++ << " ";// 5 4 3 2 1 反向遍历
	cout << endl;

	list<int>::iterator it3 = l1.end();
	do
	{
		cout << *(--it3) << " ";// begin+end,反向遍历
	} while (it3 != l1.begin());
	cout << endl;

	list<int>::reverse_iter
举报

相关推荐

0 条评论