您的位置:首页 > 其它

list容器介绍

2016-04-26 09:10 260 查看


list容器介绍

相对于vector容器的连续线性空间,list是一个双向链表,它有一个重要性质:插入操作和删除操作都不会造成原有的list迭代器失效,每次插入或删除一个元素就配置或释放一个元素空间。也就是说,对于任何位置的元素插入或删除,list永远是常数时间。

常用函数


(1) 构造函数

list<Elem> c:创建一个空的list

list<Elem> c1(c2):复制另一个同类型元素的list

list<Elem>c(n):创建n个元素的list,每个元素值由默认构造函数确定

list<Elem>c(n,elem):创建n个元素的list,每个元素的值为elem

list<Elem>c(begin,end):由迭代器创建list,迭代区间为[begin,end)


(2) 大小、判断函数

Int size() const:返回容器元素个数

bool empty() const:判断容器是否为空,若为空则返回true


(3) 增加、删除函数

void push_back(const T& x):list元素尾部增加一个元素x

void push_front(const T& x):list元素首元素钱添加一个元素X

void pop_back():删除容器尾元素,当且仅当容器不为空

void pop_front():删除容器首元素,当且仅当容器不为空

void remove(const T& x):删除容器中所有元素值等于x的元素

void clear():删除容器中的所有元素

iterator insert(iterator it, const T& x ):在迭代器指针it前插入元素x,返回x迭代器指针

void insert(iterator it,size_type n,const T& x):迭代器指针it前插入n个相同元素x

void insert(iterator it,const_iterator first,const_iteratorlast):把[first,last)间的元素插入迭代器指针it前

iterator erase(iterator it):删除迭代器指针it对应的元素

iterator erase(iterator first,iterator last):删除迭代器指针[first,last)间的元素


(4) 遍历函数

iterator begin():返回首元素的迭代器指针

iterator end():返回尾元素之后位置的迭代器指针

reverse_iterator rbegin():返回尾元素的逆向迭代器指针,用于逆向遍历容器

reverse_iterator rend():返回首元素前一个位置的迭代器指针

reference front():返回首元素的引用

reference back():返回尾元素的引用


(5) 操作函数

void sort():容器内所有元素排序,默认是升序

template<class Pred>void sort(Pred pr):容器内所有元素根据预断定函数pr排序

void swap(list& str):两list容器交换功能

void unique():容器内相邻元素若有重复的,则仅保留一个

void splice(iterator it,list& li):队列合并函数,队列li所有函数插入迭代指针it前,x变成空队列

void splice(iterator it,list& li,iterator first):队列li中移走[first,end)间元素插入迭代指针it前

void splice(iterator it,list& li,iterator first,iterator last):x中移走[first,last)间元素插入迭代器指针it前

void reverse():反转容器中元素顺序


基本操作示例:

#include "stdafx.h"

#include<iostream>

#include<string>

#include<list>

using namespace std;

typedef list<string> LISTSTR;

int _tmain(int argc, _TCHAR* argv[])

{

LISTSTR test;

test.push_back("back");

test.push_back("middle");

test.push_back("front");

cout<<test.front()<<endl;

cout<<*test.begin()<<endl;

cout<<test.back()<<endl;

cout<<*(test.rbegin())<<endl;

test.pop_front();

test.pop_back();

cout<<test.front()<<endl;

return 0;

}

程序运行结果如下:



从上述代码可以看出list首尾元素的增加和删除都是非常容易的,test.front()相当于string& s=test.front(),返回了首元素的引用;test.begin()相当于list<string>::iterator it=test.begin(),返回了首元素的迭代器指针,因此test.front()于*test.begin()的结果是一致的。

[cpp] view
plain copy

#include "stdafx.h"

#include<iostream>

#include<string>

#include<list>

using namespace std;

typedef list<int> LISTINT;

int _tmain(int argc, _TCHAR* argv[])

{

LISTINT test;

for(int i=0;i<5;i++)

{

test.push_back(i+1);

}

LISTINT::iterator it = test.begin();

for(;it!=test.end();it++)

{

cout<<*it<<"\t";

}

cout<<endl;

//reverse show

LISTINT::reverse_iterator rit = test.rbegin();

for(;rit!=test.rend();rit++)

{

cout<<*rit<<"\t";

}

cout<<endl;

return 0;

}

程序运行结果如下:



正向迭代器与逆向迭代器表示形式是不一样的,前者是iterator,后者是reverse_iterator。逆向显示并不会改变元素在容器中的位置,只是逆向显示。

[cpp] view
plain copy

#include "stdafx.h"

#include<iostream>

#include<string>

#include<list>

using namespace std;

typedef list<int> LISTINT;

int _tmain(int argc, _TCHAR* argv[])

{

LISTINT test;

test.push_back(1);

test.push_back(5);

test.push_back(3);

test.push_back(10);

LISTINT test2;

test2.push_back(2);

test2.push_back(8);

test2.push_back(6);

test2.push_back(9);

test.sort();

test2.sort();

test.merge(test2);

for(LISTINT::iterator it = test.begin();it!=test.end();it++)

{

cout<<*it<<"\t";

}

cout<<endl;

cout<<test.size()<<"\t"<<test2.size()<<endl;

return 0;

}

上面的代码展示了sort merge和splice的使用,程序运行结果如下:



从允许结果可以看出,两个链表merge合并前,一般都已经俺升序排好序,合并后的链表仍然是升序排列。merge操作是数据移动操作,不是复制操作,因此t1.merge(t2)表示把test2中所有元素依次移动并插入到源链表test的适当位置,test增加了多少个元素,test2就减少了多少个元素。若用test.splice(test.begin(),test2)代替程序中的test.merge(test2),其余不变,就能看出splice的特点。splice()完成的是拼接功能,也是数据移动操作,不慎复制操作。test.splice(test.begin(),test2)表明把test2中所有元素整体地移动到原始链表test的首元素前,test增加了多少个元素,test2就减少了多少个元素。如上述代码所述,test,test2排序后,test={1,3,5,10},test2={2,6,8,9}.
test.splice(test.begin(),test2)后,test={2,6,8,9,1,3,5,10},test2={};test.merge(test2)后,test={1,2,3,5,6,8,9,10},test2={}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: