List

目录

List简介:

List的常见接口:

list的常见构造:

list的容量操作:

size:

empty:

list的遍历操作:

begin+end:

范围for:

list的修改操作:

assign:

push_front:

pop_front:

push_back:

pop_back:

insert:

erase:

swap:

resize:

romove:

unique:

sort:

merge:

reserve:


List简介:

https://cplusplus.com/reference/list/list/?kw=listicon-default.png?t=N7T8https://cplusplus.com/reference/list/list/?kw=list

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

List的常见接口:

list的常见构造:

explicit list (const allocator_type& alloc = allocator_type());
explicit list (size_type n, const value_type& val = value_type());
template <class InputIterator>  list (InputIterator first, InputIterator last);
list (const list& x);
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l1{ 1,3,2,3,4,5,7,6 };
	list<int> l2(4, 100);
	list<int> l3(l2.begin(), l2.end());
	list<int> l4(l3);
    return 0;
}

list的容量操作:

size:

返回列表容器中元素的个数

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l1{ 1,2,3,4,5 };
	cout << l1.size() << endl;
    return 0;
}

empty:

返回列表容器是否为空(即它的大小是否为0)

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l1{ 1,2,3,4,5 };
	cout << l1.empty() << endl;
    return 0;
}

list的遍历操作:

begin+end:

begin获取第一个模版类型的迭代器+end获得最后一个字符下一个位置的迭代器

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	list<int>::iterator it1 = l1.begin();
	while (it1 != l1.end())
	{
		cout << *it1++ << " ";
	}
	cout << endl;
    return 0;
}

范围for:

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	for(auto e :l1)
    {
        cout << e << " "; 
    }
	cout << endl;
    return 0;
}

list的修改操作:

assign:

将新内容分配给列表容器,替换其当前内容,并相应地修改其大小

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	cout << l1.size() << endl;
	l1.assign(7, 100);

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	cout << l1.size() << endl;
    return 0;
}

push_front:

在列表的开头插入一个新元素,就在它当前的第一个元素之前。val的内容被复制(或移动)到插入的元素中

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	cout << l1.size() << endl;
	l1.push_front(0);

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	cout << l1.size() << endl;
    return 0;
}

pop_front:

删除列表容器中的第一个元素,有效地将其大小减小1

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	cout << l1.size() << endl;
	l1.pop_front();

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	cout << l1.size() << endl;
    return 0;
}

push_back:

在列表容器的最后一个当前元素之后,在其末尾添加一个新元素。val的内容被复制(或移动)到新元素中

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	cout << l1.size() << endl;
	l1.push_back(0);

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	cout << l1.size() << endl;
    return 0;
}

pop_back:

删除列表容器中的最后一个元素,有效地将容器大小减少一个

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	cout << l1.size() << endl;
	l1.pop_back();

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	cout << l1.size() << endl;
    return 0;
}

insert:

通过在指定位置的元素之前插入新元素来扩展容器
这有效地通过插入的元素数量增加了列表的大小

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	list<int>::iterator it1 = l1.begin();
	it1++;
	l1.insert(it1, 2);

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
    return 0;
}

erase:

从列表容器中移除单个元素(position)或一段元素([first,last))
这有效地减少了容器的大小,通过删除元素的数量,这些元素被销毁

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	list<int>::iterator it1 = l1.begin();
	it1++;

	l1.erase(it1);
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
    return 0;
}

swap:

用x的内容交换容器的内容,x是另一个相同类型的列表。大小可能不同
在调用this成员函数之后,this容器中的元素是调用之前在x中的元素,而x的元素是在this容器中的元素。所有迭代器、引用和指针对于交换后的对象仍然有效

#include <iostream>
#include <list>
using namaspace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	list<int>l2{ 6,7,8,9,10 };
	l1.swap(l2);

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
    return 0;
}

resize:

调整容器的大小,使其包含n个元素
如果n小于当前容器的大小,则内容将被减少到前n个元素,并删除超出的元素(并销毁它们)
如果n大于当前容器的大小,则通过在末尾插入所需的元素来扩展内容,以达到n的大小。如果指定了val,则将新元素初始化为val的副本,否则将其进行值初始化

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	l1.resize(100);
	l1.resize(7,100);

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
    return 0;
}

romove:

从容器中移除与val比较结果相等的所有元素。调用这些对象的析构函数,并按移除的元素数量减少容器大小

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{ 1,2,3,4,5 };
	l1.remove(2);

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
    return 0;
}

unique:

从容器中每个连续相等元素组中除去第一个元素以外的所有元素

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{1, 1, 2, 3, 4, 4, 5};
	l1.sort(greater<int>());
    l1.unique();

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
    return 0;
}

sort:

对列表中的元素进行排序,改变它们在容器中的位置

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{1, 1, 2, 3, 4, 4, 5};
	l1.sort(greater<int>());

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
    return 0;
}

reserve:

反转列表中所包含元素的顺序

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int>l1{1, 1, 2, 3, 4, 4, 5};
	l1.reverse();

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
    return 0;
}

  • 52
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 48
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值