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提供了两个参数
|
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成员函数 ,描述就不翻译了,保持原汁原味。
Member | Where defined | Description |
---|---|---|
value_type | Container | The type of object, T, stored in the list. |
pointer | Container | Pointer to T. |
reference | Container | Reference to T |
const_reference | Container | Const reference to T |
size_type | Container | An unsigned integral type. |
difference_type | Container | A signed integral type. |
iterator | Container | Iterator used to iterate through a list. |
const_iterator | Container | Const iterator used to iterate through a list. |
reverse_iterator | Reversible Container | Iterator used to iterate backwards through a list. |
const_reverse_iterator | Reversible Container | Const iterator used to iterate backwards through a list. |
iterator begin() | Container | Returns an iterator pointing to the beginning of the list. |
iterator end() | Container | Returns an iterator pointing to the end of the list. |
const_iterator begin() const | Container | Returns a const_iterator pointing to the beginning of thelist. |
const_iterator end() const | Container | Returns a const_iterator pointing to the end of the list. |
reverse_iterator rbegin() | Reversible Container | Returns a reverse_iterator pointing to the beginning of the reversed list. |
reverse_iterator rend() | Reversible Container | Returns a reverse_iterator pointing to the end of the reversed list. |
const_reverse_iterator rbegin() const | Reversible Container | Returns a const_reverse_iterator pointing to the beginning of the reversed list. |
const_reverse_iterator rend() const | Reversible Container | Returns a const_reverse_iterator pointing to the end of the reversed list. |
size_type size() const | Container | Returns 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() const | Container | Returns the largest possible size of the list. |
bool empty() const | Container | true if the list's size is 0. |
list() | Container | Creates an empty list. |
list(size_type n) | Sequence | Creates a list with n elements, each of which is a copy ofT(). |
list(size_type n, const T& t) | Sequence | Creates a list with n copies of t. |
list(const list&) | Container | The copy constructor. |
template <class InputIterator> list(InputIterator f, InputIterator l) [2] | Sequence | Creates a list with a copy of a range. |
~list() | Container | The destructor. |
list& operator=(const list&) | Container | The assignment operator |
reference front() | Front Insertion Sequence | Returns the first element. |
const_reference front() const | Front Insertion Sequence | Returns the first element. |
reference back() | Sequence | Returns the last element. |
const_reference back() const | Back Insertion Sequence | Returns the last element. |
void push_front(const T&) | Front Insertion Sequence | Inserts a new element at the beginning. |
void push_back(const T&) | Back Insertion Sequence | Inserts a new element at the end. |
void pop_front() | Front Insertion Sequence | Removes the first element. |
void pop_back() | Back Insertion Sequence | Removes the last element. |
void swap(list&) | Container | Swaps the contents of two lists. |
iterator insert(iterator pos, const T& x) | Sequence | Inserts x before pos. |
template <class InputIterator> void insert(iterator pos, InputIterator f, InputIterator l) [2] | Sequence | Inserts the range [f, l) before pos. |
void insert(iterator pos, size_type n, const T& x) | Sequence | Inserts n copies of x before pos. |
iterator erase(iterator pos) | Sequence | Erases the element at position pos. |
iterator erase(iterator first, iterator last) | Sequence | Erases the range [first, last) |
void clear() | Sequence | Erases all of the elements. |
void resize(n, t = T()) | Sequence | Inserts or erases elements at the end such that the size becomesn. |
void splice(iterator pos, list& L) | list | See below. |
void splice(iterator pos, list& L, iterator i) | list | See below. |
void splice(iterator pos, list& L, iterator f, iterator l) | list | See below. |
void remove(const T& value) | list | See below. |
void unique() | list | See below. |
void merge(list& L) | list | See below. |
void sort() | list | See below. |
bool operator==(const list&, const list&) | Forward Container | Tests two lists for equality. This is a global function, not a member function. |
bool operator<(const list&, const list&) | Forward Container | Lexicographical 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 );
下面一个例子:
今天到此结束,准备准备打算自己用C++实现数据结构敬请期待