STL 顺序容器之表

38 篇文章 0 订阅

目录


表容器使用双向链表实现的。


头文件:

#include <list>


构造方法:

  1. list<type> lst                                        创建一个没有任何元素的列表
  2. list<type> lst(otherLst)                      用另一个类型相同列表初始化该列表
  3. list<type> lst(size)                              初始化一个固定size的列表
  4. list<type> lst(n, element)                  初始化n个相同元素的列表
  5. list<type> lst(begin,end)                   初始化列表中的某一段元素,从begin 到 end - 1
列表的特有操作(列表是 顺序容器顺序容器容器的共有操作):

  1. lst.assign(n,elem)                    赋值n个元素的拷贝给列表
  2. lst.assign(beg,end)                  赋值一段迭代器的值给列表
  3. lst.push_front(elem)                添加一个元素在开头
  4. lst.pop_front()                            删除第一个元素
  5. lst.front()                                     返回第一个元素(不检测容器是否为空)
  6. lst.back()                                    返回最后一个元素(不检测容器是否为空)
  7. lst.remove(elem)                      删除所有等于elem的元素
  8. lst.remove_if(oper)                  条件删除
  9. lst.unique()                                删除重复元素(相邻位置)
  10. lst.unique(oper)                        条件删除重复元素(相邻位置)
  11. lst.splice(pos,lst2)                   将lst2所有元素移动到lst指定位置后,操作完成,lst2为空
  12. lst.splice(pos,lst2,pos2)         将lst2指定位置以后的元素移动到lst指定位置后
  13. lst.splice(pos,lst2,beg,end)   将lst2中一段位置的元素移到lst指定位置后
  14. lst.sort()                                      升序排列
  15. lst.sort(oper)                              条件升序排列
  16. lst.merge(lst2)                           有序合并,合并后lst2 为空
  17. lst.merge(lst2,oper)                  条件有序合并,合并后lst2 为空
  18. lst.reverse()                                倒置
示例代码:

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

using namespace std;

int main() {
	list<int> intList1, intList2, intList3, intList4;
	ostream_iterator<int> screen(cout, " ");

	// 从 intList1 的尾部添加元素
	intList1.push_back(23);
	intList1.push_back(58);
	intList1.push_back(23);
	intList1.push_back(23);
	intList1.push_back(36);
	intList1.push_back(23);
	intList1.push_back(15);
	intList1.push_back(98);
	intList1.push_back(23);

	// 输出元素
	cout << "intList1 :" << endl;
	copy(intList1.begin(),intList1.end(),screen);
	cout << endl;

	// 将intList1中元素拷贝到intList2中,深拷贝
	intList2 = intList1;
	cout << "intList2 :" << endl;
	copy(intList2.begin(),intList2.end(),screen);
	cout << endl;

	// 删除相邻位置的重复元素
	intList1.unique();

	// 输出元素
	cout << "intList1 去掉相邻位置的重复元素 :" << endl;
	copy(intList1.begin(),intList1.end(),screen);
	cout << endl;

	// 排序
	intList2.sort();
	cout << "intList2 排序:" << endl;
	copy(intList2.begin(),intList2.end(),screen);
	cout << endl;

	// 先排序可以去掉所有的重复元素
	intList2.unique();
	cout << "intList2 排序后去掉相邻位置的重复元素:" << endl;
	copy(intList2.begin(),intList2.end(),screen);
	cout << endl;

	// 从 intList3 的尾部添加元素
	intList3.push_back(13);
	intList3.push_back(28);
	intList3.push_back(26);
	intList3.push_back(123);
	intList3.push_back(198);

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

	// 从 intList4 的尾部添加元素
	intList4.push_back(-2);
	intList4.push_back(-7);
	intList4.push_back(-8);

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

	// intList4 中的所有元素移入intList3中
	intList3.splice(intList3.begin(),intList4);
	cout << "intList3 中开始位置移入intList4:" << endl;
	copy(intList3.begin(),intList3.end(),screen);
	cout << endl;
	cout << "intList4 :" << endl;
	copy(intList4.begin(),intList4.end(),screen);
	cout << endl;

	// intList3 排序
	intList3.sort();
	cout << "intList3 排序:" << endl;
	copy(intList3.begin(),intList3.end(),screen);
	cout << endl;

	// intList2有序合并intList3
	intList2.merge(intList3);
	cout << "intList2有序合并intList3 后 intList2:" << endl;
	copy(intList2.begin(),intList2.end(),screen);
	cout << endl;
	cout << "intList3 :" << endl;
	copy(intList3.begin(),intList3.end(),screen);
	cout << endl;


	return 0;
}

运行结果:

intList1 :
23 58 23 23 36 23 15 98 23
intList2 :
23 58 23 23 36 23 15 98 23
intList1 去掉相邻位置的重复元素 :
23 58 23 36 23 15 98 23
intList2 排序:
15 23 23 23 23 23 36 58 98
intList2 排序后去掉相邻位置的重复元素:
15 23 36 58 98
intList3 :
13 28 26 123 198
intList4 :
-2 -7 -8
intList3 中开始位置移入intList4:
-2 -7 -8 13 28 26 123 198
intList4 :


intList3 排序:
-8 -7 -2 13 26 28 123 198
intList2有序合并intList3 后 intList2:
-8 -7 -2 13 15 23 26 28 36 58 98 123 198
intList3 :


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yours风之恋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值