C++ STL学习之list

LIST 是一个双向链表。

因为本人的C++的编程时间有限,目前还没怎么用到list,在数据结构中,list是一个比较重要的。

首先复习一下什么是双向链表。双向链表是一种每个节点都有两个指针,分别直接的指向了直接前驱和直接后驱。这种方式对访问一个节点的前后都是十分方便快捷的。向前向后搜索的时间都是为常量时间。

并且在链表的头和尾部插入元素的时间都是常量时间。

List有以下优点:

  • Efficient insertion and removal of elements anywhere in the container (constant time).
  • Efficient moving elements and block of elements within the container or even between different containers (constant time).
  • Iterating over the elements in forward or reverse order (linear time).

C++ stl中list提供了两个参数

template < class T, class Allocator = allocator<T> > class list;

Where the template parameters have the following meanings:

  • T: 数据类型
  • Allocator: Type of the allocator object used to define the storage allocation model. By default, theallocator class template for type T is used, which defines the simplest memory allocation model and is value-independent.

In the reference for the list member functions, these same names are assumed for the template parameters.

Public base classes

None.

list成员函数 ,描述就不翻译了,保持原汁原味。

MemberWhere definedDescription
value_typeContainerThe type of object, T, stored in the list.
pointerContainerPointer to T.
referenceContainerReference to T
const_referenceContainerConst reference to T
size_typeContainerAn unsigned integral type.
difference_typeContainerA signed integral type.
iteratorContainerIterator used to iterate through a list.
const_iteratorContainerConst iterator used to iterate through a list.
reverse_iteratorReversible ContainerIterator used to iterate backwards through a list.
const_reverse_iteratorReversible ContainerConst iterator used to iterate backwards through a list.
iterator begin()ContainerReturns an iterator pointing to the beginning of the list.
iterator end()ContainerReturns an iterator pointing to the end of the list.
const_iterator begin() constContainerReturns a const_iterator pointing to the beginning of thelist.
const_iterator end() constContainerReturns a const_iterator pointing to the end of the list.
reverse_iterator rbegin()Reversible ContainerReturns a reverse_iterator pointing to the beginning of the reversed list.
reverse_iterator rend()Reversible ContainerReturns a reverse_iterator pointing to the end of the reversed list.
const_reverse_iterator rbegin() constReversible ContainerReturns a const_reverse_iterator pointing to the beginning of the reversed list.
const_reverse_iterator rend() constReversible ContainerReturns a const_reverse_iterator pointing to the end of the reversed list.
size_type size() constContainerReturns the size of the list. Note: you should not assume that this function is constant time. It is permitted to beO(N), where N is the number of elements in the list. If you wish to test whether alist is empty, you should write L.empty() rather than L.size() == 0.
size_type max_size() constContainerReturns the largest possible size of the list.
bool empty() constContainer true if the list's size is 0.
list()ContainerCreates an empty list.
list(size_type n)SequenceCreates a list with n elements, each of which is a copy ofT().
list(size_type n, const T& t)SequenceCreates a list with n copies of t.
list(const list&)ContainerThe copy constructor.
template <class InputIterator>
list(InputIterator f, InputIterator l)
[2]
SequenceCreates a list with a copy of a range.
~list()ContainerThe destructor.
list& operator=(const list&)ContainerThe assignment operator
reference front()Front Insertion SequenceReturns the first element.
const_reference front() constFront Insertion SequenceReturns the first element.
reference back()SequenceReturns the last element.
const_reference back() constBack Insertion SequenceReturns the last element.
void push_front(const T&)Front Insertion SequenceInserts a new element at the beginning.
void push_back(const T&)Back Insertion SequenceInserts a new element at the end.
void pop_front()Front Insertion SequenceRemoves the first element.
void pop_back()Back Insertion SequenceRemoves the last element.
void swap(list&)ContainerSwaps the contents of two lists.
iterator insert(iterator pos, const T& x)SequenceInserts x before pos.
template <class InputIterator>
void insert(iterator pos, 
            InputIterator f, 
            InputIterator l)
[2]
SequenceInserts the range [f, l) before pos.
void insert(iterator pos, 
            size_type n, const T& x)
SequenceInserts n copies of x before pos.
iterator erase(iterator pos)SequenceErases the element at position pos.
iterator erase(iterator first, iterator last)SequenceErases the range [first, last)
void clear()SequenceErases all of the elements.
void resize(n, t = T())SequenceInserts or erases elements at the end such that the size becomesn.
void splice(iterator pos, list& L)listSee below.
void splice(iterator pos, 
            list& L,
            iterator i)
listSee below.
void splice(iterator pos,
            list& L, 
            iterator f, iterator l)
listSee below.
void remove(const T& value)listSee below.
void unique()listSee below.
void merge(list& L)listSee below.
void sort()listSee below.
bool operator==(const list&, 
                const list&)
Forward ContainerTests two lists for equality. This is a global function, not a member function.
bool operator<(const list&, 
               const list&)
Forward ContainerLexicographical comparison. This is a global function, not a member function.

在这里,着重强调一下list的构造函数,内部形式如下

explicit list ( const Allocator& = Allocator() ); explicit list ( size_type n, const T& value = T(), const Allocator& = Allocator() ); template < class InputIterator > list ( InputIterator first, InputIterator last, const Allocator& = Allocator() ); list ( const list<T,Allocator>& x );


下面一个例子:

// list.cpp : 定义控制台应用程序的入口点。 // by wallwind at sunrise 2011/10/20 #include "stdafx.h" #include <list> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { list<int> mylist1; list<int> mylist2(4,10); list<int> mylist3(mylist2.begin(),mylist2.end()); list<int> mylist4(mylist3); int myints[]={1,3,4,5,6}; list<int> mylist5(myints,myints+sizeof(myints)/sizeof(int)); for (list<int>::iterator it=mylist5.begin();it!=mylist5.end();it++) { cout<<*it<<" "; } cout<<endl; cout<<"mylist5 size: "<<mylist5.size()<<endl; for (int i=1;i<=4;i++) { mylist1.push_back(i); } mylist2.clear(); for (int i=1; i<=3; i++) mylist2.push_back(i*10); list<int> ::iterator it1=mylist1.begin(); ++it1; mylist1.splice(it1,mylist2);//mylist1: 1 10 20 30 2 3 4 //mylist2 (empty) //"it" still points to 2 (the 5th element) mylist2.splice(mylist2.begin(),mylist1,it1);// mylist1: 1 10 20 30 3 4 // mylist2: 2 // "it" is now invalid. it1=mylist1.begin(); advance(it1,3); //it points to "30" return 0; }


今天到此结束,准备准备打算自己用C++实现数据结构敬请期待

更多内容:http://blog.csdn.net/wallwind

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值