stl list容器


list容器
list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。
使用list容器之前必须加上头文件:#include;
list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespace std;
主要函数
push_back(num) 在末尾增加一个元素。
push_front(num)在开始位置增加一个元素。
insert(pos,num)在pos位置插入元素num。
insert(pos,n,num)在pos位置插入n个元素num。
insert(pos,beg,end)在pos位置插入区间为[beg,end)的元素。
pop_back()删除末尾的元素。
pop_front()      删除第一个元素。
erase(pos)删除pos位置的元素。
remove(num) 删除链表中匹配num的元素。
remove_if(comp) 删除条件满足的元素,参数为自定义的回调函数。
clear()清除链表c中的所有元素。
front()返回链表c的第一个元素。
back()返回链表c的最后一个元素。
empty()判断链表是否为空。
size()返回链表c中实际元素的个数。
reverse() 反转链表
unique() 删除相邻的元素
sort()将链表排序,默认升序
sort(comp) 自定义回调函数实现自定义排序
splice实现list拼接的功能。将源list的内容部分或全部元素删除,拼插入到目的list。
begin()返回指向链表第一个元素的迭代器。
end()返回指向链表最后一个元素之后的迭代器。
rbegin()返回逆向链表的第一个元素,即c链表的最后一个数据。
rend()返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。
c1.merge(c2)合并2个有序的链表并使之有序,从新放到c1里,释放c2。
c1.merge(c2,comp)合并2个有序的链表并使之按照自定义规则排序之后从新放到c1中,释放c2。
c1.swap(c2); 将c1和c2交换。
swap(c1,c2); 同上。
重载运算符
operator==
operator!=
operator<
operator<=
operator>
operator>=

#include
#include
using namespace std;
int main()
{
	int a[]={1,2,3,90},b[]={1,6,8,9};
	list q1(a,a+4),q2(b,b+4);
	cout<<(q1==q2)<q2)<::iterator it=q1.begin();it!=q1.end();it++)
	{
		cout<<*it<<" ";
	}
	cout<::iterator it=q2.begin();it!=q2.end();it++)
	{
		cout<<*it<<" ";
	}
	cout<::iterator it=q1.begin();it!=q1.end();it++)
	{
		cout<<*it<<" ";
	}
	cout<::iterator it=q2.begin();it!=q2.end();it++)
	{
		cout<<*it<<" ";
	}
	cout<

 P1177 【模板】快速排序

#include
using namespace std;
int main()
{
	list  l;
	int n;
	cin>>n;
	for (int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		l.push_back(x);
	}
	l.sort();
	list::iterator it;
	for (it=l.begin();it!=l.end();it++)
	{
		cout<<*it<<" ";
	}
	
}