STL中序列容器之list

STL序列容器之list

本文介绍序列容器中的list,list容器是通过双向链表实现的。因此,list中的每个元素有一个直接前驱和直接后继(除了第一个和最后一个元素),通过前面的知识我们可知,链表不是一个可以随机访问的数据结构(不像数组),因此,假设我们要访问第5个元素时,我们必须首先遍历前4个元素。

 

类list的定义,对list的各种操作函数定义的实现包含在头文件list中,因此,在程序中使用list时,必须包含如下的语句:

#include<list>

像其他容器类一样,类list包含几个构造器,因此,当声明一个list对象时,有好几种方式来进行初始化。如下表所示:

 

前面介绍了所有容器的通用操作以及所有序列容器的通用操作,这些操作适用于list。然而,除了这些通用的操作以外,下表描述了list容器特有的操作(listCont是list类型容器):

下例展示了list容器的各种操作:

#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>

using namespace std;

int main()
{
	list<int> intList1, intList2, intList3, intList4;
	ostream_iterator<int> screen(cout, " ");
	intList1.push_back(23);
	intList1.push_back(58);
	intList1.push_back(58);
	intList1.push_back(58);
	intList1.push_back(36);
	intList1.push_back(15);
	intList1.push_back(93);
	intList1.push_back(98);
	intList1.push_back(58);
	cout << "intList1: ";
	copy(intList1.begin(), intList1.end(), screen);
	cout << "\n====================\n" << endl;

	intList2 = intList1;

	cout << "intList2: ";
	copy(intList2.begin(), intList2.end(), screen);
	cout << "\n====================\n" << endl;

	intList1.unique();

	cout << "After removing the onsecutive "
		<< "duplicates, " << endl
		<< "	intList1: ";
	copy(intList1.begin(), intList1.end(), screen);
	cout << "\n====================\n" << endl;

	intList2.sort();
	cout << "After sorting, intList2: ";
	copy(intList2.begin(), intList2.end(), screen);
	cout << "\n====================\n" << endl;

	intList3.push_back(13);
	intList3.push_back(23);
	intList3.push_back(25);
	intList3.push_back(136);
	intList3.push_back(198);

	cout << "intList3: ";
	copy(intList3.begin(), intList3.end(), screen);
	cout << "\n====================\n" << endl;

	intList4.push_back(-2);
	intList4.push_back(-7);
	intList4.push_back(-8);

	cout << "intList4: ";
	copy(intList4.begin(), intList4.end(), screen);
	cout << "\n====================\n" << endl;

	intList3.splice(intList3.begin(), intList4);
	cout << "After moving the elements of "
		<< "intList4 into intList3, " << endl
		<< "	intList3: ";
	copy(intList3.begin(), intList3.end(), screen);
	cout << "\n====================\n" << endl;

	intList3.sort();
	cout << "After sorting, intList3: ";
	copy(intList3.begin(), intList3.end(), screen);
	cout << "\n====================\n" << endl;

	intList2.merge(intList3);

	cout << "After merging intList2 and "
		<< "intList3, " << endl
		<< "	intList2: ";
	copy(intList2.begin(), intList2.end(), screen);
	cout << "\n====================\n" << endl;

	intList2.unique();
	cout << "After removing the consecutive "
		<< "duplicates, " << endl
		<< "	intList2: ";
	copy(intList2.begin(), intList2.end(), screen);
	cout << "\n====================\n" << endl;

	return 0;
}

输出为:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值