您的位置:首页 > 编程语言 > C语言/C++

c++ list容器

2014-04-21 00:55 375 查看
头文件#include <list>

template < class T, class Alloc = allocator<T> > class list;
list容器的实现一定程度上是基于双链表的,
1.list与vector和deque的区别:
  1)list不支持随机存取,要访问第n个元素,必须先遍历前n-1个元素才能访问第n个元素。因此,list没有下标[]操作,也没有at()接口。
  2)list插入删除速度较快,在任何位置上插入删除效率都是一样的,不需要移动元素,只需要管理相应的指针就行。
    另外,list的插入删除无论在什么情况下都不会使其他元素的指针,引用,迭代器失效。
  3)list容器不提供对capacity和内存维护相关的接口,因为list容器中的每个元素分配自己的内存空间,当元素删除时,其对应的内存空间销毁。
list容器跟 vector、deque容器都属于序列容器(sequence containers), 所以大部分的操作接口还是类似的,像创建(create)、复制(copy)、
销毁(destroy)、大小(size)、比较(compare)、赋值 (assignment)、迭代器(iterator)等都基本类似。
2.list的操作
常用函数:(参数列表省略了。。。)

迭代器:

begin()

返回迭代器开始(公共成员函数)

end()

返回迭代器结束(公共成员函数)

rbegin()

反向迭代器返回开始反向(公共成员函数)

rend()

反向迭代器返回反向结束(公共成员函数)

cbegin()

const_iterator回到开始(公共成员函数)

cend()

返回const_iterator结束(公共成员函数)

crbegin()

返回const_reverse_iterator逆转开始(公共成员函数)

crend()

返回const_reverse_iterator扭转结束(公共成员函数)

容量capacity:

empty()

测试容器是否为空(公共成员函数)

size()

返回的大小(公共成员函数)

max_size

返回最大大小(公共成员函数)

元素访问:

front()

访问第一个元素(公共成员函数)

back()

访问的最后一个元素(公共成员函数)

修饰符Modifiers:

assign()

新内容分配给容器(公共成员函数)

emplace_front()

在开始构建和插入元素(公共成员函数)

push_front()

插入元素开始(公共成员函数)

pop_front()

删除第一个元素(公共成员函数)

emplace_back()

最后构造和插入元素(公共成员函数)

push_back()

末尾添加元素(公共成员函数)

pop_back()

删除最后一个元素(公共成员函数)

emplace()

构造和插入元素(公共成员函数)

insert()

插入元素(公共成员函数)

erase()

删除元素(公共成员函数)

swap()

交换内容(公共成员函数)

resize()

改变大小(公共成员函数)

clear()

清空内容(公共成员函数)

操作:

splice()

将元素从列表中列出(公共成员函数)

remove()

删除元素与特定的值(公共成员函数)

remove_if()

删除元素满足条件(公共成员函数模板)

unique()

删除重复的值(公共成员函数)

merge()

合并排序的列表(公共成员函数)

sort()

排序元素的容器(公共成员函数)

reserve()

改变元素的顺序(公共成员函数)

Observers:

get_allocator()

得到分配器(公共成员函数)

实例:

#include <iostream>
#include <list>
using namespace std;

int main()
{
list<char> coll;
for (char c ='a'; c <='z'; c++)
coll.push_back(c);
while (!coll.empty()) {
cout << coll.front() << ' ';
coll.pop_front();
}
cout << endl;
return 0;
}


输出:a b c d e f g h i j k l m n o p q r s t u v w x y z

实例1:

#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;

void printLists(const list<int>& l1,const list<int>& l2)
{
cout << "list1: ";
copy(l1.begin(),l1.end(),ostream_iterator<int>(cout," "));
cout << endl << "list2: ";
copy(l2.begin(),l2.end(),ostream_iterator<int>(cout," "));
cout << endl <<endl;
}

int main()
{
list<int> list1,list2;
for (int i =0; i <6; ++i) {
list1.push_back(i);
list2.push_front(i);
}

printLists(list1,list2);

list2.splice(find(list2.begin(),list2.end(),3),list1);
printLists(list1,list2);

list2.splice(list2.end(),list2,list2.begin());
printLists(list1,list2);

list2.sort();
list1 = list2;

list2.unique();
printLists(list1,list2);

list1.merge(list2);
printLists(list1,list2);
}


输出:

list1: 0 1 2 3 4 5
list2: 5 4 3 2 1 0

list1:
list2: 5 4 0 1 2 3 4 5 3 2 1 0

list1:
list2: 4 0 1 2 3 4 5 3 2 1 0 5

list1: 0 0 1 1 2 2 3 3 4 4 5 5
list2: 0 1 2 3 4 5

list1: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
list2:

实例2:

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

int main()
{
list<int> lst;
list<int>::iterator it;

cout << "lst.size() = "<<lst.size()<<", lst.max_size() = "<<lst.max_size()<<endl;

cout <<"lst.push_front()前插:";
for (int i =10; i <90; i+=10)
lst.push_front(i);
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl;
cout << "lst.push_back()后插:" ;
for (int i =10; i <90; i+=10)
lst.push_back(i);
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

list<int> lst2(lst);
cout << "list<int> lst2(lst):";
for (it =lst2.begin(); it !=lst2.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

list<int> lst3(5,*(lst.begin()));
cout <<"lst3(5,*(lst.begin())):";
for (it =lst3.begin(); it !=lst3.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

int i =0;
for (it =lst.begin();i <6 && it !=lst.end(); ++it,++i);
list<int>::iterator it2(it);
it =lst.begin();
list<int> lst4(it,it2);
cout << "lst4(it,it2):";
for (it =lst4.begin(); it !=lst4.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

cout <<"lst ==lst2 ?=" <<(lst ==lst2) <<endl;
cout << "lst !=lst2 ? =" << (lst !=lst2) <<endl;
cout << "lst < lst3 ?= " << (lst <lst3) <<endl;
cout << "lst >=lst3 ?=" << (lst >=lst3) <<endl;
cout <<endl;

list<int> lst5;
lst5.assign(6,*it2);
cout << "lst.assign(6,*it2):";
for (it =lst5.begin(); it !=lst5.end(); ++it)
cout << *it << ' ';
cout <<endl << endl;

cout << "lst4.front():" <<lst4.front() <<endl;
cout << "lst4.back():" <<lst4.back() <<endl;
cout <<endl;

cout << "swap(lst4,lst4):" <<endl;
cout << "lst4:";
for (it =lst4.begin(); it !=lst4.end(); ++it)
cout << *it << ' ';
cout <<endl;
cout << "lst5:";
for (it =lst5.begin(); it !=lst5.end(); ++it)
cout << *it << ' ';
cout <<endl;
swap(lst4,lst5);
cout << "swap(lst4,lst5)后:" <<endl;
cout << "lst4:";
for (it =lst4.begin(); it !=lst4.end(); ++it)
cout << *it << ' ';
cout <<endl;
cout << "lst5:";
for (it =lst5.begin(); it !=lst5.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

cout << "lst.pop_front():";
lst.pop_front();
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl;
cout <<"lst.pop_back():";
lst.pop_back();
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

lst.remove(10);
lst.remove(20);
cout << "lst.remove(10 and 20):";
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

it =lst.begin();
lst.insert(++it,3,*(lst.begin()));
cout << "lst.insert(++it,3,*(lst.begin())):";
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

it =lst.begin();
lst.erase(it);
cout << "lst.erase(it):";
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

for (i =1,it =lst.begin(); i <4 && it !=lst.end(); ++it,++i);
it2 =it;
it = lst.begin();
lst.erase(it,it2);
cout << "lst.erase(it,it2):";
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;;

lst.resize(12,*(lst.begin()));
cout << "lst.resize(12,*(lst.begin())):";
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

lst.sort();
cout << "lst.sort():" ;
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

lst.remove_if(bind2nd(greater<int>(),50));
cout << "lst.remove_if(bind2nd(greater<int>(),50)):";
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

cout <<"lst4:";
for (it =lst4.begin(); it !=lst4.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;
it =lst.begin();

lst.splice(++it,lst4);
cout << "lst.splice(it.lst4):";
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

lst.unique();
cout << "lst.unique():";
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;

lst.merge(lst5);
cout << "lst.merge(lst5):";
for (it =lst.begin(); it !=lst.end(); ++it)
cout << *it << ' ';
cout <<endl <<endl;
}


输出:

lst.size() = 0, lst.max_size() = 357913941
lst.push_front()前插:80 70 60 50 40 30 20 10
lst.push_back()后插:80 70 60 50 40 30 20 10 10 20 30 40 50 60 70 80

list<int> lst2(lst):80 70 60 50 40 30 20 10 10 20 30 40 50 60 70 80

lst3(5,*(lst.begin())):80 80 80 80 80

lst4(it,it2):80 70 60 50 40 30

lst ==lst2 ?=1
lst !=lst2 ? =0
lst < lst3 ?= 1
lst >=lst3 ?=0

lst.assign(6,*it2):20 20 20 20 20 20

lst4.front():80
lst4.back():30

swap(lst4,lst4):
lst4:80 70 60 50 40 30
lst5:20 20 20 20 20 20
swap(lst4,lst5)后:
lst4:20 20 20 20 20 20
lst5:80 70 60 50 40 30

lst.pop_front():70 60 50 40 30 20 10 10 20 30 40 50 60 70 80
lst.pop_back():70 60 50 40 30 20 10 10 20 30 40 50 60 70

lst.remove(10 and 20):70 60 50 40 30 30 40 50 60 70

lst.insert(++it,3,*(lst.begin())):70 70 70 70 60 50 40 30 30 40 50 60 70

lst.erase(it):70 70 70 60 50 40 30 30 40 50 60 70

lst.erase(it,it2):60 50 40 30 30 40 50 60 70

lst.resize(12,*(lst.begin())):60 50 40 30 30 40 50 60 70 60 60 60

lst.sort():30 30 40 40 50 50 60 60 60 60 60 70

lst.remove_if(bind2nd(greater<int>(),50)):30 30 40 40 50 50

lst4:20 20 20 20 20 20

lst.splice(it.lst4):30 20 20 20 20 20 20 30 40 40 50 50

lst.unique():30 20 30 40 50

lst.merge(lst5):30 20 30 40 50 80 70 60 50 40 30
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: