参考:http://blog.csdn.net/whz_zb/article/details/6831817
list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素。在STL中,list和vector一样,是两个常被使用的容器。和vector不一样的是,list不支持对元素的任意存取。list中提供的成员函数与vector类似,不过list提供对表首元素的操作push_front、pop_front,这是vector不具备的。和vector另一点不同的是,list的迭代器不会存在失效的情况,他不像vector会保留备份空间,在超过容量额度时重新全部分配内存,导致迭代器失效;list没有备份空间的概念,出入一个元素就申请一个元素的空间,所以它的迭代器不会失效
list每次增加一个元素,不存在重新申请内存的情况,它的成本是恒定的。而vector每当增加关键元素的时候,都需要重新申请新的更大的内存空间,会调用元素的自身的复制构造函数,存在构造成本。在销毁旧内存的时候,会调用析构函数,存在析构成本。所以在存储复杂类型和大量元素的情况下,list比vector更有优势!
List是一个双向链表,双链表既可以向前又向后链接他的元素。
List将元素按顺序储存在链表中. 与 向量(vector)相比, 它允许快速的插入和删除,但是随机访问却比较慢。
List头文件:
#include<list>
assign() 给list赋值
back() 返回最后一个元素
begin() 返回指向第一个元素的迭代器
clear() 删除所有元素
empty() 如果list是空的则返回true
end() 返回末尾的迭代器
erase() 删除一个元素
front() 返回第一个元素
get_allocator() 返回list的配置器
insert() 插入一个元素到list中
max_size() 返回list能容纳的最大元素数量
merge() 合并两个list
pop_back() 删除最后一个元素
pop_front() 删除第一个元素
push_back() 在list的末尾添加一个元素
push_front() 在list的头部添加一个元素
rbegin() 返回指向第一个元素的逆向迭代器
remove() 从list删除元素
remove_if() 按指定条件删除元素
rend() 指向list末尾的逆向迭代器
resize() 改变list的大小
reverse() 把list的元素倒转
size() 返回list中的元素个数
sort() 给list排序
splice() 合并两个list
swap() 交换两个list
unique() 删除list中重复的元素
参考:http://blog.csdn.net/lskyne/article/details/10418823
#include#include #include
#include using namespace std; //创建一个list容器的实例LISTINT typedef list LISTINT; //创建一个list容器的实例LISTCHAR typedef list LISTCHAR; void main() { //用list容器处理整型数据 //用LISTINT创建一个名为listOne的list对象 LISTINT listOne; //声明i为迭代器 LISTINT::iterator i; //从前面向listOne容器中添加数据 listOne.push_front (2); listOne.push_front (1); //从后面向listOne容器中添加数据 listOne.push_back (3); listOne.push_back (4); //从前向后显示listOne中的数据 cout<<"listOne.begin()--- listOne.end():"<
结果:
listOne.begin()--- listOne.end():
1 2 3 4listOne.rbegin()---listOne.rend():4 3 2 1Sum=10------------------listTwo.begin()---listTwo.end():B A x yThe maximum element in listTwo is: yPress any key to continue
#include#include using namespace std; typedef list INTLIST; //从前向后显示list队列的全部元素 void put_list(INTLIST list, char *name) { INTLIST::iterator plist; cout << "The contents of " << name << " : "; for(plist = list.begin(); plist != list.end(); plist++) cout << *plist << " "; cout<
list3: "<<(list1>list3)<
结果:
The contents of list1 :
The contents of list2 : 6 6 6 6 6 6 6 6 6 6The contents of list3 : 6 6 6 6 6 6 6 6 6list1.push_back(2) and list1.push_back(4):The contents of list1 : 2 4list1.push_front(5) and list1.push_front(7):The contents of list1 : 7 5 2 4list1.insert(list1.begin()+1,3,9):The contents of list1 : 7 9 9 9 5 2 4list1.front()=7list1.back()=4list1.pop_front() and list1.pop_back():The contents of list1 : 9 9 9 5 2list1.erase(++list1.begin()):The contents of list1 : 9 9 5 2list2.assign(8,1):The contents of list2 : 1 1 1 1 1 1 1 1list1.max_size(): 1073741823list1.size(): 4list1.empty(): 0The contents of list1 : 9 9 5 2The contents of list3 : 6 6 6 6 6 6 6 6 6list1>list3: 1list1<list3: 0The contents of list1 : 2 5 9 9The contents of list1 : 2 6 6 6 6 6 6 6 6 6 5 9 9The contents of list3 :Press any key to continue