您的位置:首页 > 其它

STL 顺序容器之表

2014-04-12 16:33 260 查看
目录

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

头文件:

#include <list>

构造方法:

list<type> lst 创建一个没有任何元素的列表
list<type> lst(otherLst) 用另一个类型相同列表初始化该列表
list<type> lst(size) 初始化一个固定size的列表
list<type> lst(n, element) 初始化n个相同元素的列表
list<type> lst(begin,end) 初始化列表中的某一段元素,从begin 到 end - 1

列表的特有操作(列表是顺序容器顺序容器容器的共有操作):

lst.assign(n,elem) 赋值n个元素的拷贝给列表
lst.assign(beg,end) 赋值一段迭代器的值给列表
lst.push_front(elem) 添加一个元素在开头
lst.pop_front() 删除第一个元素
lst.front() 返回第一个元素(不检测容器是否为空)
lst.back() 返回最后一个元素(不检测容器是否为空)
lst.remove(elem) 删除所有等于elem的元素
lst.remove_if(oper) 条件删除
lst.unique() 删除重复元素(相邻位置)
lst.unique(oper) 条件删除重复元素(相邻位置)
lst.splice(pos,lst2) 将lst2所有元素移动到lst指定位置后,操作完成,lst2为空
lst.splice(pos,lst2,pos2) 将lst2指定位置以后的元素移动到lst指定位置后
lst.splice(pos,lst2,beg,end) 将lst2中一段位置的元素移到lst指定位置后
lst.sort() 升序排列
lst.sort(oper) 条件升序排列
lst.merge(lst2) 有序合并,合并后lst2 为空
lst.merge(lst2,oper) 条件有序合并,合并后lst2 为空
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 :
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: